cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Check out the Dropbox cheat sheet for getting started in 2021 here!

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Uploading files from a file server to DropBox

Uploading files from a file server to DropBox

Helpful | Level 5

I've been adding some code to upload a file from my server to a specific folder in Dropbox but nothing successful. I'm using Yii2 advanced framework. (Php, javascript and html code in it).

These are the different options that I have used:

  1. $app=new DropboxApp($consumerKey,$consumerSecret,$accessToken);
    $dropbox=new Dropbox($app);

    // Check if file was uploaded
    if (isset($_FILES["file"])) {
    // File to Upload
    $file = $_FILES['file'];

        // File Path
        $fileName = $file['name'];
        $filePath = $file['tmp_name'];

        try {
       // Create Dropbox File from Path
       $dropboxFile = new DropboxFile($filePath);

      // Upload the file to Dropbox
      $uploadedFile = $dropbox->upload($dropboxFile, "/" . $fileName,     ['autorename' => true]);

     // File Uploaded
    echo $uploadedFile->getPathDisplay();
    } catch (DropboxClientException $e) {
    echo $e->getMessage();
    }
    }

     2. This one creates the folder but does not save the file...

 $inputData=Yii::$app->request->post();
$dropboxFile = new DropboxFile($filePath);
 $folderName = $inputData['Model']['file];
 $file = $dropbox->upload($dropboxFile, "/".$fileName, ['autorename' => true]);
$folderName = $inputData['Model']['file'];
 $folder = $dropbox->createFolder($dropboxFile,"/".$folderName);
 $path = 'https:confused face:/www.dropbox.com/home/.../...' .$file->path_display;
 $dropbox_path = array('dropbox_path'=>$path);
 $inputData['Model']['dropbox_path'] = $path;
$model->load($inputData);
 $model->save(false);
 }

Any suggestion? Other code that I can implement to it?
Thanks in advance. 

1 Accepted Solution

Accepted Solutions

Re: Uploading files from a file server to DropBox

Dropboxer

Based on this code, it looks like you've switched to using this other "Dropbox Uploader" library:

https:confused face:/github.com/jakajancar/DropboxUploader

We do not recommend using this. From the its own readme:

"Its development was started before Dropbox released their API, and to work, it scrapes their website. So you can and probably should use their API now as it is much more stable."

I recommend using a third party library that actually uses the API, like you were before, or calling the API directly.

View solution in original post

9 Replies 9

Re: Uploading files from a file server to DropBox

Dropboxer
 
I’ve gone ahead and moved your post to the developer section of the Forum - perhaps some like-minded users will have some ideas to share on this with you. 
 
Cheers! 

Lusil
Community Moderator @ Dropbox
dropbox.com/support


Heart Did this post help you? If so, please give it a Like below.
Still stuck? Ask me a question!
Tips & Tricks Find new ways to stay in flow or share your tips on how you work smarter with Dropbox.

Re: Uploading files from a file server to DropBox

Dropboxer

From the code you provided, it looks like you're using this third party library: 

https:confused face:/github.com/kunalvarma05/dropbox-php-sdk

We can't provide support for third party libaries, as we didn't make them, but we'll be happy to help with any issues you're having with the Dropbox API itself.

Whenever you make a Dropbox API call, you should receive a response indicating if the call succeeded or failed. It sounds like you're having trouble uploading to Dropbox. What does the 'upload' method return or throw?

Re: Uploading files from a file server to DropBox

Helpful | Level 5

I can change the code from third party to the official Dropbox Api.
What I want to do is to select the file that I want to upload and click the Save/Upload button to save it in Dropbox in a specific folder directory.

The upload method that I have right now is not throwing any error. It works as if it's uploading the file but it returns to the same page without any warning or flash message.

Re: Uploading files from a file server to DropBox

Dropboxer

If the method is not throwing an error, what is it returning? (I.e., `$uploadedFile` in your code #1, or `$file` in your code #2.)`

Re: Uploading files from a file server to DropBox

Helpful | Level 5

$uploadedFile in code #1.

It seems the variable is not taking the file that I selected so the variable is coming empty.

Re: Uploading files from a file server to DropBox

Dropboxer

I'm not sure I understand what you mean when you say "the variable is not taking the file that I selected".

Do you mean $uploadedFile is null after the upload method runs? It sounds like the method didn't successfully run. You may need to debug your code more, or double check if you're getting any other output or error. 

Re: Uploading files from a file server to DropBox

Helpful | Level 5

You actually got to the point... the method did not succesfully run.

I change the code to a different one and now it's running but it's not saving in the desire folder: using DropboxUploader

<?php
if ($_POST) {
require 'DropboxUploader.php';


try {
// Rename uploaded file to reflect original name
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK)
throw new Exception('File was not successfully uploaded from your computer.');

$tmpDir = uniqid('/tmp/DropboxUploader-');
if (!mkdir($tmpDir))
throw new Exception('Cannot create temporary directory!');

if ($_FILES['file']['name'] === "")
throw new Exception('File name not supplied by the browser.');

$tmpFile = $tmpDir.'/'.str_replace("/\0", '_', $_FILES['file']['name']);
if (!move_uploaded_file($_FILES['file']['tmp_name'], $tmpFile))
throw new Exception('Cannot rename uploaded file!');

// Enter your Dropbox account credentials here
$uploader = new DropboxUploader('email', 'password');
$uploader->upload($tmpFile, $_POST['dest']);

echo '<span style="color: green;font-weight:face with tongue:old;margin-left:393px;">File successfully uploaded to my Dropbox!</span>';
} catch(Exception $e) {
echo '<span style="color: red;font-weight:face with tongue:old;margin-left:393px;">Error: ' . htmlspecialchars($e->getMessage()) . '</span>';
}

// Clean up
if (isset($tmpFile) && file_exists($tmpFile))
unlink($tmpFile);

if (isset($tmpDir) && file_exists($tmpDir))
rmdir($tmpDir);
}
?>

Re: Uploading files from a file server to DropBox

Dropboxer

Based on this code, it looks like you've switched to using this other "Dropbox Uploader" library:

https:confused face:/github.com/jakajancar/DropboxUploader

We do not recommend using this. From the its own readme:

"Its development was started before Dropbox released their API, and to work, it scrapes their website. So you can and probably should use their API now as it is much more stable."

I recommend using a third party library that actually uses the API, like you were before, or calling the API directly.

View solution in original post

Re: Uploading files from a file server to DropBox

Helpful | Level 5

I'll try my best. If it start to give me errors or problems for upload a file then I will recurr here to the forum and ask about it.

Thanks 

Polls
Are you starting new work habits this year?

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?