dropbox-php-sdk
dropbox-php-sdk copied to clipboard
UploadFileFromString?
Would it be possible to implement an "UploadFileFromString" function, similar to the function of the same name in the original, official dropbox-sdk-php library?
Thanks!
Yes, this would be useful.
I just had to do this. Faced some issues using php://memory and php://temp as a workaround, so fell back to a temp file. It worked fine.
The $data variable here is my string, and $path is the intended path on Dropbox, eg. /path/to/filename.txt
$file_id = uniqid();
$tmpfname = tempnam( wp_upload_dir()['path'], $file_id );
$handle = fopen( $tmpfname, "w" );
fwrite($handle, $data);
$dropboxFile = DropboxFile::createByStream($path, $handle, DropboxFile::MODE_WRITE);
$metadata = $this->dropbox->upload( $dropboxFile, $path, ['mode' => 'overwrite']);
I would be glad to implement this :)
If the file is created temporarily in the disk, it's handler should be closed and the file deleted after it's use.
$content = 'Hello world!';
$tmpfname = tempnam(__DIR__, uniqid());
$handle = fopen($tmpfname, "w");
fwrite($handle, $content);
$dpstream = DropboxFile::createByStream('', $handle, 'w');
fclose($handle);
try
{
$metadata = $dropbox->upload($dpstream, $dropboxFile, ['mode' => 'overwirte']);
}
catch(Exception $e)
{
return false;
}
if (realpath($tmpfname) && realpath($tmpfname) !== $tmpfname)
{
if (is_writable($tmpfname))
unlink(realpath($tmpfname));
}