Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
Informatica P.
10 years agoNew member | Level 1
Is posible upload file whit PHP and get public share link to file?
Hello,
I want upload a file whit API PHP. But i want get the public link share to my users download the file from their home.
Is posible?
4 Replies
- Greg-DB10 years ago
Dropbox Community Moderator
Dropbox does offer an API that enables programmatic listing, uploading, downloading, and sharing of files, so this is possible. You can find everything you need to get started with the Dropbox API, including documentation, tutorials, and SDKs here. You'll need to register an app here.
App authorization is processed using OAuth. You can find more information about using OAuth in our OAuth guide.
We don't currently have an official PHP SDK for Dropbox API v2, so if you're using PHP, you can call the API v2 endpoints directly.
To upload a file, you can use the /files/upload endpoint. To get a shared link, you can use the /sharing/create_shared_link_with_settings endpoint.
Or you can use the official PHP SDK for Dropbox API v1:
https://www.dropbox.com/developers-v1/core/sdks/php
https://www.dropbox.com/developers-v1/core/start/php
https://dropbox.github.io/dropbox-sdk-php/api-docs/v1.1.x/In that SDK, you can upload files using the getFile method, and share them using the createShareableLink method.
- David D.8610 years agoNew member | Level 1
This is the code I have for PHP v2 for uploading a file, using curl functions, but I'm at a loss as to how to describe what the POST data should look like.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$token = "yourtoken";
$host = "https://api.dropboxapi.com/";
//set headers
$headers = array('Authorization: Bearer '.$token,
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: {"path":"/test.txt","mode":"add"}',
);$f = fopen("test.txt", "rb");curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// What should the POST data look like? Using the file handle? That doesn't work.
curl_setopt($ch, CURLOPT_POSTFIELDS, $f);
$response = curl_exec($ch);
fclose($f);
echo "<br />File Upload Response: <br />";
var_export($response);
echo "<br />File UPload HTTP CODE: <br />";
var_export(curl_getinfo($ch, CURLINFO_HTTP_CODE));
curl_close($ch); - Steve M.10 years ago
Dropbox Staff
Your URL is wrong... it should be https://content.dropboxapi.com/2/files/upload.
There may be other ways, but one way to post the content of a local file would be to read it into memory first. E.g., this worked for me:
curl_setopt($ch, CURLOPT_POSTFIELDS, fread($f, filesize("test.txt"))); - Encoder IT Limited4 years agoNew member | Level 2
Yes, it is possible. Please follow the code below:
$token = 'YOUR-TOKEN'; // oauth token $path = 'YOUR-FILE-HERE'; // Ex. text.txt or demo.mp4 /** * Upload file to dropbox */ $fp = fopen($path, 'rb'); $size = filesize($path); $ext = end(explode('.', $path)); $d_path = uniqid().'.'.$ext; $cheaders = array('Authorization: Bearer '.$token, 'Content-Type: application/octet-stream', 'Dropbox-API-Arg: {"path":"/'.$d_path.'", "mode":"add"}'); $ch = curl_init('https://content.dropboxapi.com/2/files/upload'); curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders); curl_setopt($ch, CURLOPT_PUT, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_INFILE, $fp); curl_setopt($ch, CURLOPT_INFILESIZE, $size); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); // echo $response; curl_close($ch); fclose($fp); /** * Get shared link */ $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => 'https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings', CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => '{ "path": "/' . $d_path . '", "settings": { "access": "viewer", "allow_download": true, "audience": "public", "requested_visibility": "public" } }', CURLOPT_HTTPHEADER => [ 'Content: application/json', 'Authorization: Bearer ' . $token, 'Content-Type: application/json' ] ]); $response = curl_exec($curl); $data = json_decode($response, true); echo $url = $data['url']; curl_close($curl);
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!