php 使用 Zend 框架 1.7.4 上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/665334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:25:24  来源:igfitidea点击:

File Upload using zend framework 1.7.4

phpzend-frameworkfile-uploadzend-form

提问by nitin

I am trying to upload a file using Zend Framework 1.7.4, but have not been successful. I have read Akrabat's tutorial, which was helpful but when i used those techniques in my project I was not able to get it to work.

我正在尝试使用 Zend Framework 1.7.4 上传文件,但没有成功。我已经阅读了Akrabat 的教程,这很有帮助,但是当我在我的项目中使用这些技术时,我无法让它工作。

回答by Pax

The link you posted is just a general Zend Framework tutorial, and hasn't been updated past ZF 1.5.

您发布的链接只是一般的 Zend 框架教程,在 ZF 1.5 之后还没有更新。

Anyway, once you get started with Zend, this is a sample of the code you would use to receive an upload. The form doing the posting must have the correct file upload components.

无论如何,一旦您开始使用 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()

回答by chiborg

Don't forget to set the enctypeattribute of the form to "multipart/form-data". If you are using Zend_Form, call

不要忘记enctype将表单的属性设置为“ multipart/form-data”。如果您使用 Zend_Form,请调用

$form->setAttrib('enctype', 'multipart/form-data');

Also note that Zend_Form::setDestinationis deprecated, use the rename filter for that:

另请注意,Zend_Form::setDestination已弃用,请为此使用重命名过滤器:

// Deprecated:
// $upload->setDestination('/tmp');
// New method:
$upload->addFilter('Rename', '/tmp');

回答by Nanhe Kumar

    $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);

You can set any destination example $photo->setLabel('Photo:')->setDestination(APPLICATION_PATH ."/../data");

您可以设置任何目标示例 $photo->setLabel('Photo:')->setDestination(APPLICATION_PATH ."/../data");