2012-05-22 38 views
1

我們在Zend框架中構建一個應用程序,並且在移動上傳的文件時遇到問題。我們通過$ filePath = $ form-> image-> getFileName()獲取文件;但是當我們試圖在其上運行move_uploaded_file時,它只是返回false。帶Zend的move_uploaded_file()不起作用

圖像成功上傳到臨時目錄,但我們無法將其移動到文件夾中。

$formData = $request->getPost(); 
       if ($form->isValid($formData)) 
       { 
             $form->image->receive(); 
             $filePath = $form->image->getFileName(); 
              move_uploaded_file($filePath,APPLICATION_PATH . '\images\new'); 
     } 

在此先感謝

編輯:

當我嘗試,我得到500 - 內部服務器錯誤:

  $upload = new Zend_File_Transfer_Adapter_Http(); 

      $upload->setDestination("C:\xx\xx\public\banners"); 

      if (!$upload->isValid()) 
      { 
       throw new Exception('Bad image data: '.implode(',', $upload->getMessages())); 
       } 

     try { 
     $upload->receive(); 
     } 
     catch (Zend_File_Transfer_Exception $e) 
     { 
      throw new Exception('Bad image data: '.$e->getMessage()); 
     } 

看來,它是「$ upload- > setDestination(「C:\ xx \ xx \ public \ banners」); 「導致崩潰

回答

2

在計算器這個等效問題&答案應該幫助你:File Upload using zend framework 1.7.4

//validate file 
//for example, this checks there is exactly 1 file, it is a jpeg and is less than 512KB 
$upload = new Zend_File_Transfer_Adapter_Http(); 
$upload->addValidator('Count', false, array('min' =>1, 'max' => 1)) 
     ->addValidator('IsImage', false, 'jpeg') 
     ->addValidator('Size', false, array('max' => '512kB')) 
     ->setDestination('/tmp'); 

if (!$upload->isValid()) 
{ 
    throw new Exception('Bad image data: '.implode(',', $upload->getMessages())); 
} 

try { 
     $upload->receive(); 
} 
catch (Zend_File_Transfer_Exception $e) 
{ 
     throw new Exception('Bad image data: '.$e->getMessage()); 
} 

//then process your file, it's path is found by calling $upload->getFilename() 

使用->receive()後您已經移動上傳文件,以便調用另一個move_uploaded_file()是毫無意義的。

+0

嗨,謝謝你的回答。我用你的建議編輯我的問題。我得到一個崩潰:( – BlackMouse

+0

@ user1251004不知道這是否會改變任何東西,但試試這個:'「C:\\ xx \\ xx \\ public \\ banners」'。注意雙斜線。 –

+0

不幸的是,但感謝您的建議 – BlackMouse