move_uploaded_file() with Zend doens't work

江枫思渺然 提交于 2019-12-09 23:23:47

问题


We're building an application within Zend framework and are having trouble moving an uploaded file. We get the file by $filePath = $form->image->getFileName(); but when we're trying to run move_uploaded_file on it, it just returns false.

The image is successfully being uploaded to the temp directory, but we can't move it into a folder.

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

Thanks in advance

EDIT:

When I try this, I get 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());
       }

It seems that it is the " $upload->setDestination("C:\xx\xx\public\banners"); " that cause the crash


回答1:


This equivalent question & answer on stackoverflow should help you out: 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()

After using ->receive() you have already moved the uploaded file so calling another move_uploaded_file() is pointless.



来源:https://stackoverflow.com/questions/10708149/move-uploaded-file-with-zend-doenst-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!