-->

move_uploaded_file() with Zend doens't

2020-07-28 08:53发布

问题:

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.