Matt Maclennan
Matt Maclennan

Reputation: 1304

Notify when dropbox file is uploaded in PHP

I have a Dropbox upload script, which works fine, however, I need to let the mail recipient know that a file has been/hasn't been uploaded to dropbox. Below is the code I am using...

<?
 if( ($_SESSION['security_code']==$_POST['security_code']) &&   (!empty($_POST['security_code'])) ) { 
 mail("[email protected]","Website Print Shop Enquiry","Form data:

 Contact Name: " . $_POST['field_1'] . " 
 Company (if applicable): " . $_POST['field_2'] . " 
 Address (optional): " . $_POST['field_3'] . " 
 Postcode: " . $_POST['field_4'] . " 
 Phone: " . $_POST['field_5'] . " 
 Email: " . $_POST['field_6'] . " 
 Dropbox File Uploaded?: " . >>RESULT HERE<< . " 
 Details of enquiry: " . $_POST['field_7'] . " ",$headers);


 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('username is here', 'password is here');
        $uploader->upload($tmpFile, $_POST['dest']);

      } catch(Exception $e) {
       }

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

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

  include("confirm-print.htm");
 }
else {
  echo "Invalid Captcha String. Please use the back button in your browser and try again, thank you.";
 }

?>

Trying to find what variable I need to access in order to show whether a file has been uploaded or not. The DropboxUploader.php file concerned is at https://github.com/jakajancar/DropboxUploader/blob/master/DropboxUploader.php Thanks for any help!

Upvotes: 0

Views: 515

Answers (2)

Matt Maclennan
Matt Maclennan

Reputation: 1304

Sorted it! Used an if statement at the top of the PHP file...

if($_FILES['file']['name']==""){
        $result = "None"; //no file was uploaded
    }
    else
    {
        $result = "Yes";
    }

Upvotes: 0

Lauris
Lauris

Reputation: 1115

Look at line 92 in DropboxUploader.php If upload is unsuccessful, then exception is thrown with message "Upload failed!".

I suggest modify this file and change Exception class to something more specific like DropboxUploadException (of course define this exception subclass first). Then modify your code like this:

try
{
    // Your old code until "$uploader->upload($tmpFile, $_POST['dest']);"
    try
    {
        $uploader->upload($tmpFile, $_POST['dest']);

        $upload_success = true;
    }
    catch(DropboxUploadException $e)
    {
        $upload_success = true;
    }

    // At the very end move your mail() function call and you can use $upload_success
    // variable to display file upload success or failure
}
catch(Exception $e)
{
    // Generic exception handling
}

Upvotes: 1

Related Questions