如何用Zend_Form_Element_File上传文件 - ZF Dream

如何用Zend_Form_Element_File上传文件

阿克 posted @ 2009年8月10日 21:21 in 海外教程 with tags Zend_Form_Element_File Zend Framework php , 4170 阅读

现在Zend Framework 1.7已经发布了,我看了看代码包中有关文件上传的类Zend_Form_Element_File,看它怎么用。下面是如何在最基本的表单中使用Zend_Form_Element_File的教程。
我决定拿过去一样的表单设置,以便让教程显得容易些。

Zend_Form_Element_File例图

让我们开始吧:(ZF Dream翻译

表单
我们扩展Zend_Form,并把它保存在application/forms目录中,这样类名就是forms_UploadForm: 

<?php

class forms_UploadForm extends Zend_Form
{
    public function __construct($options = null)
    {
        parent::__construct($options);
        $this->setName('upload');
        $this->setAttrib('enctype', 'multipart/form-data');

        $description = new Zend_Form_Element_Text('description');
        $description->setLabel('Description')
                  ->setRequired(true)
                  ->addValidator('NotEmpty');

        $file = new Zend_Form_Element_File('file');
        $file->setLabel('File')
            ->setDestination(BASE_PATH . '/data/uploads')
            ->setRequired(true);

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Upload');

        $this->addElements(array($description, $file, $submit));

    }
}

首先,我们设置表单的名称和编码属性,以便让文件可以上传。表单包含2项:一个名为“描述”的文本框,一个名为“文件”的上传框,当然提交按钮是必须的。这里一点都不复杂。
Zend_Form_Element_File类有一个setDestination()方法,用来告诉后面的Zend_File_Transfer_Adapter_Http我们想把上传后的文件存在哪里。在本例中,我们准备存在data/uploads。(ZF Dream翻译

控制器与视图
控制器写法很标准: 

<?php

class IndexController extends Zend_Controller_Action 
{
    public function indexAction() 
    {
        $this->view->headTitle('Home');
        $this->view->title = 'Zend_Form_Element_File Example';
        $this->view->bodyCopy = "<p>Please fill out this form.</p>";

        $form = new forms_UploadForm();

        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {

                // success - do something with the uploaded file
                $uploadedData = $form->getValues();
                $fullFilePath = $form->file->getFileName();

                Zend_Debug::dump($uploadedData, '$uploadedData');
                Zend_Debug::dump($fullFilePath, '$fullFilePath');

                echo "done";
                exit;

            } else {
                $form->populate($formData);
            }
        }

        $this->view->form = $form;

    }
}

 视图views/scripts/index.phtml很普通: 

<h1><?= $this->title; ?></h1>
<?= $this->bodyCopy; ?>
<?= $this->form; ?>

如果表单验证正确,$uploadedData数组中会含有表单域的值和上传文件的文件名。注意这个文件名并不完整,所以如果你需要文件的完整路径,得用类中的getFileName()方法取得。(ZF Dream翻译

小结
这就是表单上传的简单例子。上传组件中确实有些BUG,需要等1.7.2以上版本解决。特别是计数验证器总不能如你所愿地工作,不要使用getValues()和receive(),因为它还没有聪明到知道不要调用move_uploaded_file()超过一次。
和过去一样,这是本例用于测试的zip文件:Zend_Form_Element_File_Example.zip(包含了Zend Framework 1.7的一部分,所以有3.9M那么大)。(ZF Dream翻译

原文:http://akrabat.com/2008/11/29/file-uploads-with-zend_form_element_file(Rob Allen)


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee