dropbox-php-sdk icon indicating copy to clipboard operation
dropbox-php-sdk copied to clipboard

UploadFileFromString?

Open KyleKotowick opened this issue 8 years ago • 3 comments

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!

KyleKotowick avatar Sep 28 '17 21:09 KyleKotowick

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']);

codewithfeeling avatar Oct 17 '17 16:10 codewithfeeling

I would be glad to implement this :)

eznix86 avatar Oct 01 '19 16:10 eznix86

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));
}

NazgulTuga avatar May 07 '20 13:05 NazgulTuga