-
Notifications
You must be signed in to change notification settings - Fork 183
Upload and Download Files
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");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
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
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
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 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 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();This method is recommended, especially when downloading large files. (Thanks @xgin!)
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