1

我们正在 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 - internal server error:

            $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"); ”导致崩溃

4

1 回答 1

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()毫无意义。

于 2012-05-22T18:54:32.987 回答