簡體   English   中英

如何在上傳文件時捕獲異常-Zend Framework 1

[英]How to catch exception in uploading file - zend framework 1

我嘗試在zend Framework 1中上傳文件時捕獲異常。

我拒絕了對該文件夾的許可,然后運行以下代碼以捕獲該異常,但它不起作用。

public function uploadImage($postedFile,$destination) {
        try {
              $imageName = $this->getFileName($postedFile); //$postedFile is same as $_FILES


        $upload = new Zend_File_Transfer();

        foreach ($upload->getFileInfo($imageName) as $info) {

            if ($info['name'] != '') {
                $ext = pathinfo($info['name'], PATHINFO_EXTENSION);
                $newName = md5(rand(1, 100).date('ymdhis') . $info['name']) . '.' . $ext;

                $upload->addFilter('Rename', $destination."/".$newName);
                if (!$upload->receive($info['name'])) {
                    return FALSE;
                }

            }
            break;
        }
          return $newName;
        } catch (Zend_File_Transfer_Exception $e) {
            throw new Exception('I want to catch this'); 
        }

    }

錯誤:

    Warning: 
move_uploaded_file(/var/www/html/glistonapp/application/../public/images/app_user_profile_picture/80d55d25c52ef4d74079cfa903288b77.png): 
failed to open stream: Permission denied in /var/www/html/glistonapp/library/Zend/File/Transfer/Adapter/Http.php on line 189 
Warning: move_uploaded_file(): Unable to move '/tmp/phpOtOLVv' to '/var/www/html/glistonapp/application/../public/images/app_user_profile_picture/80d55d25c52ef4d74079cfa903288b77.png' in /var/www/html/glistonapp/library/Zend/File/Transfer/Adapter/Http.php on line 189

似乎您在Zend_File_Transfer對象上調用的方法不會引發異常。

沒有拋出異常,您將無法“捕獲” try塊中的任何內容。 相反,您應該檢查所調用函數的返回值以確定是否存在問題。

請參閱API參考,該參考將告訴您哪些方法拋出異常: http : //framework.zend.com/apidoc/1.12/classes/Zend_File_Transfer_Adapter_Abstract.html

您的錯誤消息也不例外,因此try / catch塊不會捕獲該錯誤消息。

您應該在目標目錄上為該錯誤消息設置適當的權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM