使用 Zend 的 move_uploaded_file() 不起作用 [英] move_uploaded_file() with Zend doens't work
问题描述
我们正在 Zend 框架内构建应用程序,但在移动上传的文件时遇到问题.我们通过 $filePath = $form->image->getFileName() 获取文件;但是当我们尝试对其运行 move_uploaded_file 时,它只会返回 false.
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');
}
提前致谢
当我尝试这个时,我得到 500 - 内部服务器错误:
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());
}
好像是"$upload->setDestination("C:\xx\xx\public\banners");" 导致崩溃
It seems that it is the " $upload->setDestination("C:\xx\xx\public\banners"); " that cause the crash
推荐答案
这个等价的问题 &stackoverflow 上的回答应该会帮助你:File Upload using zend framework 1.7.4
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()
在使用 ->receive()
之后,你已经移动了上传的文件,所以调用另一个 move_uploaded_file()
是没有意义的.
After using ->receive()
you have already moved the uploaded file so calling another move_uploaded_file()
is pointless.
这篇关于使用 Zend 的 move_uploaded_file() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!