Skip to content

Upload and Download Files

Kunal Varma edited this page Jun 30, 2016 · 9 revisions

Upload and Download Files

The Dropbox File

The DropboxFile class represents a file to be uploaded. Before uploading a file to Dropbox, you need to instantiate a DropboxFile class. It's not mandatory, to pass a DropboxFile class instance, since you can pass the path to the file instead.

use Kunnu\Dropbox\DropboxFile;

$dropboxFile = new DropboxFile(__DIR__ . "/Hello-World.txt");

Auto File Upload

Upload a File to Dropbox, either in a single request or in chunks, automatically. This is the recommended method to upload a file.

Example

$file = $dropbox->upload($dropboxFile, "/My-Hello-World.txt", ['autorename' => true]);

//Uploaded File
$file->getName();

The upload() method will return the FileMetadata of the uploaded file.

Simple File Upload

Upload a File to Dropbox in a single request.

Example

$file = $dropbox->simpleUpload($dropboxFile, "/My-Hello-World.txt", ['autorename' => true]);

//Uploaded File
$file->getName();

The simpleUpload() method will return the FileMetadata of the uploaded file.

For available options see: https://www.dropbox.com/developers/documentation/http/documentation#files-upload

Chunked File Upload

Upload a File to Dropbox in chunks.

Example

$file = $dropbox->uploadChunked($dropboxFile, "/My-Hello-World.txt");

//Uploaded File
$file->getName();

The uploadChunked() method will return the FileMetadata of the uploaded file.

Upload File by URL

Save URL

Save a specified URL into a file in user's Dropbox.

Example

$asyncJobID = $dropbox->saveUrl("/my-logo.png", 'http://mywebsite.com/wp-content/uploads/2016/06/logo.png');

The saveUrl() method will return a asyncJobID (Async Job ID). The asyncJobID string is an id that can be used to obtain the status of the asynchronous job using the checkJobStatus method.

For available options see: https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-save_url

Check Job Status

Check the status of a saveUrl job.

Example

$asyncJobID = $dropbox->saveUrl("/my-logo.png", 'http://mywebsite.com/wp-content/uploads/2016/06/logo.png');

//Check Status
$status = $dropbox->checkJobStatus($asyncJobID);

//Job Successful. File saved.
if ($status instanceof FileMetadata) {
    $file = $status;
    echo $file->getName();
} elseif ($status === "in_progress") {
    echo "Processing job...";
} elseif ($status === "failed") {
    echo "Job Failed!";
} else {
    echo $status;
}

The checkJobStatus() method will either return the FileMetadata of the saved file upon success or the job status, which could be either in_progress or failed.

For available options see: https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-save_url-check_job_status

Download File

Download a file.

Example

$file = $dropbox->download("/my-logo.png");

//File Contents
$contents = $file->getContents();

//Save file contents to disk
file_put_contents(__DIR__ . "/logo.png", $contents);

//Downloaded File Metadata
$metadata = $file->getMetadata();

//Name
$metadata->getName();

The download() method will return the File model.

For available options see: https://www.dropbox.com/developers/documentation/http/documentation#files-download

Clone this wiki locally