Skip to content

Upload and Download Files

Kunal Varma edited this page Oct 14, 2017 · 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.

Create DropboxFile by File Path

A DropboxFile instance can be created by passing the path to the local file as the first parameter and optionally passing the access mode (Read or Write) as the second parameter to the constructor. The createByPath() static method can also be used for the same.

use Kunnu\Dropbox\DropboxFile;

$pathToLocalFile = __DIR__ . "/Hello-World.txt";

$dropboxFile = new DropboxFile($pathToLocalFile);

// OR

$mode = DropboxFile::MODE_READ;
$dropboxFile = new DropboxFile($pathToLocalFile, $mode);

// OR
// Note: The `$mode` parameter is mandatory for this method.
$dropboxFile = DropboxFile::createByPath($pathToLocalFile, $mode);

Create DropboxFile by File Stream

A DropboxFile instance can also be created by using a file stream.

use Kunnu\Dropbox\DropboxFile;

$pathToLocalFile = __DIR__ . "/Hello-World.txt";

// Automatically create stream through file path
$dropboxFile = DropboxFile::createByStream("/Hello-World.txt", $pathToLocalFile);

// OR

// Create stream through file stream
$fileStream = fopen($pathToLocalFile, DropboxFile::MODE_READ);
$dropboxFile = DropboxFile::createByStream("/Hello-World.txt", $fileStream);

Kudos to @jnstr for the createByPath() and createByStream() methods.

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.

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

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 details & 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 size (bytes)
$fileSize = 400000000;

//Chunk Size
$chunkSize = $filSize / 4;

$file = $dropbox->uploadChunked($dropboxFile, "/game-of-thrones-fan-trailer.mp4", $filesize, $chunkSize, ['autorename' => true]);

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

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

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

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 details & 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 details & 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();

Downloading directly to file

This method is recommended, especially when downloading large files. (Kudos to @xgin for the contribution)

Example

// Download and the save the file at the given path
$file = $dropbox->download("/my-large-file.mp4", '/path-to-save-file-to.mp4');

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

//Name
$metadata->getName();

The download() method will return the File model.

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

<< Detailed Usage Guide