move_uploaded_file()与Zend不起作用

时间:2012-05-22 18:47:18

标签: php zend-framework

我们正在Zend框架中构建应用程序,并且无法移动上传的文件。我们通过$ filePath = $ form-> image-> getFileName();获取文件。但是当我们尝试在其上运行move_uploaded_file时,它只返回false。

图像已成功上传到临时目录,但我们无法将其移动到文件夹中。

   $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“); “导致崩溃的原因

1 个答案:

答案 0 :(得分:2)

这个等同的问题& stackoverflow上的答案可以帮助你: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()毫无意义。