cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Update: Find information on Dropbox support during COVID-19 here
Close

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted

Is posible upload file whit PHP and get public share link to file?

New member | Level 1
New member | Level 1

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?

3 Replies 3

Re: Is posible upload file whit PHP and get public share link to file?

Dropboxer
Dropboxer

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.

Highlighted

Re: Is posible upload file whit PHP and get public share link to file?

New member | Level 1
New 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);
Highlighted

Re: Is posible upload file whit PHP and get public share link to file?

Dropboxer
Dropboxer

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")));

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Polls
Have you been keeping up good habits while working from home?
We've created a working from home cheat sheet to help keep up on those habits, check it out here.
Need more support?