public function BackupMigrateDropboxAPI::file_upload in Backup and Migrate Dropbox 7.2
Same name and namespace in other branches
- 7.3 backup_migrate_dropbox.dropbox_api.inc \BackupMigrateDropboxAPI::file_upload()
- 7 backup_migrate_dropbox.dropbox_api.inc \BackupMigrateDropboxAPI::file_upload()
Uploads a file to the given path.
If the upload is larger then:
- what Dropbox can handle per request.
- or the internal memory available to PHP
The upload is split into multiple smaller chunks, otherwise it is uploaded in 1 part.
Parameters
string $file: Name of local file to upload its contents from.
string $path: Path on Dropbox (including the file name) to upload the file contents to.
Return value
object The json decoded response.
Throws
\RuntimeException
File
- ./
backup_migrate_dropbox.dropbox_api.inc, line 118 - backup_migrate_dropbox.api.inc
Class
- BackupMigrateDropboxAPI
- @file backup_migrate_dropbox.api.inc
Code
public function file_upload($file, $path) {
// Cut PHP memory limit by 10% to allow for other in memory data.
$php_memory_limit = intval($this
->byte_size(ini_get('memory_limit')) * 0.9);
// Dropbox currently has a 150M upload limit per transaction.
$dropbox_upload_limit = $this
->byte_size('150M');
// For testing or in case the 10% leeway isn't enough allow a smaller upload
// limit as an advanced setting. This variable has no ui but can be set with
// drush or through the variable module.
$manual_upload_limit = $this
->byte_size(variable_get('backup_migrate_dropbox_upload_limit', '150M'));
// Use the smallest value for the max file size.
$max_file_size = min($php_memory_limit, $dropbox_upload_limit, $manual_upload_limit);
// File.
$file_size = filesize($file);
// If the file size is greater than
if ($file_size > $max_file_size) {
// Open file.
$file_handle = fopen($file, 'rb');
if (!$file_handle) {
throw new RuntimeException('Cannot open backup file (1).');
}
// Start.
$content = fread($file_handle, $max_file_size);
if (!$content) {
throw new RuntimeException('Cannot read backup file (2).');
}
$this
->_file_upload_session_start($content);
// Append.
while (!feof($file_handle)) {
// Get content.
$content = fread($file_handle, $max_file_size);
if (!$content) {
throw new RuntimeException('Cannot read backup file (3).');
}
$this
->_file_upload_session_append($content);
}
// Finish.
$result = $this
->_file_upload_session_finish($path);
}
else {
$content = file_get_contents($file);
if (!$content) {
throw new RuntimeException('Cannot open backup file (4).');
}
$result = $this
->_file_upload_upload($path, $content);
}
return $result;
}