2011-02-01 55 views
1

我想在Zend-framework中上傳圖片。使用Zend Framework上傳圖片?

在Application_Form_Test.php我寫下面的代碼....

uploadImage = new Zend_Form_Element_File('uploadImage'); 
$uploadImage->setLabel("Upload Image ") 
      ->setRequired(true)    
      ->addValidator('Extension', false, 'jpeg,png') 
      ->getValidator('Extension')->setMessage('This file type is not supportted.'); 

在testAction()我寫下面的代碼.....

$upload = new Zend_File_Transfer_Adapter_Http(); 
$upload->addValidator('Size', false, 52428800, 'image'); 
$upload->setDestination('uploads'); 
$files = $upload->getFileInfo(); 
foreach ($files as $file => $info) { 
    if ($upload->isValid($file)) { 
     $upload->receive($file); 
    } 
} 

代碼成功,但我跑我沒有得到該圖像到目標文件夾?

可能是什麼問題....? 請幫我.....

在此先感謝....

回答

0

附加說明:如果你看Zend_Form_Element_File->receive(),你會看到isValid()被調用,所以沒有必要使用它來控制你的控制器。以下是我的工作內容:

if ($upload->receive()) { 
    if ($upload->getFileName() && !file_exists($upload->getFileName())) { 
     throw new Exception('The upload should have worked, but somehow did not!'); 
} 
} else { 
    throw new Exception(implode(PHP_EOL, $upload->getErrors()) . implode(PHP_EOL, $upload->getErrorMessages())); 
}