我正在尝试使用 Zend Framework 1.7.4 上传文件,但没有成功。我已经阅读了 Akrabat 的教程,这很有帮助,但是当我在我的项目中使用这些技术时,我无法让它工作。
nitin
问问题
27811 次
3 回答
25
您发布的链接只是一个一般的 Zend Framework 教程,在 ZF 1.5 之后没有更新。
无论如何,一旦您开始使用 Zend,这就是您将用来接收上传的代码示例。进行发布的表单必须具有正确的文件上传组件。
//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()
于 2009-04-26T14:20:38.340 回答
8
不要忘记将enctype
表单的属性设置为“ multipart/form-data
”。如果您使用 Zend_Form,请调用
$form->setAttrib('enctype', 'multipart/form-data');
另请注意,Zend_Form::setDestination
已弃用,请为此使用重命名过滤器:
// Deprecated:
// $upload->setDestination('/tmp');
// New method:
$upload->addFilter('Rename', '/tmp');
于 2009-07-08T19:06:52.530 回答
0
$this->setAction('/sandbox/example/form')->setEnctype('multipart/form-data')->setMethod('post');
$photo = new Zend_Form_Element_File('photo');
$photo->setLabel('Photo:')->setDestination(APPLICATION_PATH ."/../public/tmp/upload");
$this->addElement($photo);
您可以设置任何目的地示例 $photo->setLabel('Photo:')->setDestination(APPLICATION_PATH ."/../data");
于 2012-10-31T10:02:45.130 回答