在PHP上传Dropbox文件时通知

时间:2013-01-09 10:28:42

标签: php dropbox

我有一个Dropbox上传脚本,工作正常,但是,我需要让邮件收件人知道文件已经/尚未上传到dropbox。以下是我正在使用的代码...

<?
 if( ($_SESSION['security_code']==$_POST['security_code']) &&   (!empty($_POST['security_code'])) ) { 
 mail("test@email.co.uk","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.";
 }

?>

尝试查找我需要访问的变量,以显示文件是否已上传。 DropboxUploader.php文件位于https://github.com/jakajancar/DropboxUploader/blob/master/DropboxUploader.php感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

查看DropboxUploader.php中的第92行如果上传不成功,则会抛出异常并显示消息&#34;上传失败!&#34;。

我建议修改此文件并将Exception类更改为更具体的类似DropboxUploadException(当然首先定义此异常子类)。然后像这样修改你的代码:

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
}

答案 1 :(得分:0)

排序了!在PHP文件的顶部使用了if语句...

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