0

我有一个 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感谢您的帮助!

4

2 回答 2

1

查看DropboxUploader.php中的第 92 行如果上传不成功,则会引发异常并显示消息“上传失败!”。

我建议修改此文件并将 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
}
于 2013-01-09T10:37:10.643 回答
0

整理好了!在 PHP 文件的顶部使用了 if 语句...

if($_FILES['file']['name']==""){
        $result = "None"; //no file was uploaded
    }
    else
    {
        $result = "Yes";
    }
于 2013-01-09T11:30:35.147 回答