Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

jary_capstone's avatar
jary_capstone
Helpful | Level 5
8 years ago
Solved

Uploading files from a file server to DropBox

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://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. 

  • Greg-DB's avatar
    Greg-DB
    8 years ago

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

    https://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.

9 Replies

Replies have been turned off for this discussion
  • Lusil's avatar
    Lusil
    Icon for Dropbox Staff rankDropbox Staff
    8 years ago
     
    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! :nerd:
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

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

    https://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?

  • jary_capstone's avatar
    jary_capstone
    Helpful | Level 5
    8 years ago

    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.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    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.)`

  • jary_capstone's avatar
    jary_capstone
    Helpful | Level 5
    8 years ago

    $uploadedFile in code #1.

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

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    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. 

  • jary_capstone's avatar
    jary_capstone
    Helpful | Level 5
    8 years ago

    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:bold;margin-left:393px;">File successfully uploaded to my Dropbox!</span>';
    } catch(Exception $e) {
    echo '<span style="color: red;font-weight:bold;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);
    }
    ?>

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

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

    https://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.

  • jary_capstone's avatar
    jary_capstone
    Helpful | Level 5
    8 years ago

    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 

About Discuss Dropbox Developer & API

Node avatar for Discuss Dropbox Developer & API
Make connections with other developers

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!