Quantcast
Channel: Clain Dsilva – Clain Dsilva's Blog
Viewing all articles
Browse latest Browse all 13

How to upload files to Dropbox with PHP put method using Curl

$
0
0

Hi all, today I will explain you about how easily you can upload a file to dropbox with PHP and http put method. First of all we will take a look at what are the requirements.

  1. PHP server with Curl extension installed [well most of it does come with curl installed]
  2. Dropbox Access Token [ you can easily generate it from your drobox api account ]

Set up your Url , Filepath and  Access Token

$file_path_str = 'FileName.ext';

$url_path_str = 'https://api-content.dropbox.com/1/files_put/auto/Foldername/'.$file_path_str; 
$header = array(
"Authorization:Bearer _YourAccessToken_",
"Content-Length:".filesize($file_path_str)
);

Initialize curl and add custom method PUT

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url_path_str);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

Read the file and make the Curl parameters

$fh_res = fopen($file_path_str, 'r');
$file_data_str = fread($fh_res, filesize($file_path_str));
rewind($fh_res);

curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Make the curl HTTP request and get the response

$curl_response_res = curl_exec ($ch);
// Print the response from server
echo $curl_response_res;
print_r(curl_getinfo($ch));
curl_close($ch);
fclose($fh_res);

Final Complete Code

<?php

$file_path_str = 'FileName.ext';

$url_path_str = 'https://api-content.dropbox.com/1/files_put/auto/Foldername/'.$file_path_str; 
$header = array(
"Authorization:Bearer _YourAccessToken_",
"Content-Length:".filesize($file_path_str)
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url_path_str);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

$fh_res = fopen($file_path_str, 'r');
$file_data_str = fread($fh_res, filesize($file_path_str));
rewind($fh_res);

curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$curl_response_res = curl_exec ($ch);

echo $curl_response_res; // Server response
print_r(curl_getinfo($ch)); // Http Response
curl_close($ch);

fclose($fh_res);

?>

Things to Remember

  • The Access Token should not be made visible to public, hence in production environment make sure you have an error free code [ including warning and notices] .
  • Make sure you do not upload very large files wile making a request from a web server, this can result in timeouts, for larger files its best to execute this code from a CLI / Bash Shell where there are no timeouts.

Hope you have enjoyed the post, Please do post your comments and suggestions.


Viewing all articles
Browse latest Browse all 13

Trending Articles