使用zend framework 1.7.4进行文件上传

nit*_*tin 5 php zend-framework file-upload zend-form

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

Pax*_*Pax 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()
Run Code Online (Sandbox Code Playgroud)


chi*_*org 8

不要忘记enctype将表单的属性设置为" multipart/form-data".如果您使用的是Zend_Form,请致电

$form->setAttrib('enctype', 'multipart/form-data');
Run Code Online (Sandbox Code Playgroud)

另请注意,Zend_Form::setDestination不推荐使用重命名过滤器:

// Deprecated:
// $upload->setDestination('/tmp');
// New method:
$upload->addFilter('Rename', '/tmp');
Run Code Online (Sandbox Code Playgroud)