【问题标题】:How to use Zend_File_Transfer?如何使用 Zend_File_Transfer?
【发布时间】:2011-10-12 13:24:34
【问题描述】:

我一直在使用普通文件上传元素来上传文件并验证它们。但最近,我发现实现 Zend_file_tranfer 可以对文件进行很多控制。

我在互联网上到处搜索,寻找一个简单的例子来开始使用它,但没有一个显示它们是如何链接到元素的。我不知道在哪里创建 Zend_File_Transfer 的对象,以及如何将它添加到元素中?基本不知道怎么用。

谁能给我一个在 zend_form 和 Zend_Controller_Action 中使用 zend_File_tranfers 的初学者示例

【问题讨论】:

  • 我希望我问了一个可以理解的问题。基本上,我想要从初学者的角度来看使用 Zend_File_Transfer 来控制文件上传的简单指南。

标签: zend-framework file-upload zend-form zend-file


【解决方案1】:

形式:

class Application_Form_YourFormName extends Zend_Form
{
    public function __construct()
    {
        parent::__construct($options);
        $this->setAction('/index/upload')->setMethod('post');
        $this->setAttrib('enctype', 'multipart/form-data');

        $upload_file = new Zend_Form_Element_File('new_file');
        $new_file->setLabel('File to Upload')->setDestination('./tmp');
        $new_file->addValidator('Count', false, 1);
        $new_file->addValidator('Size', false, 67108864);
        $new_file->addValidator('Extension', false, Array('png', 'jpg'));

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

        $this->addElements(array($upload_file, $submit));
    }
}

在控制器中:

class Application_Controller_IndexController extends Zend_Controller_Action
{
    public function uploadAction()
    {
        $this->uform = new Application_Form_YourFormName();
        $this->uform->new_file->receive();
        $file_location = $this->uform->new_file->getFileName();

        // .. do the rest...
    }
}

【讨论】:

    【解决方案2】:

    当您创建表单时,请在表单中执行以下操作:

    $image = $this->getElement('image');
    //$image = new Zend_Form_Element_File();
    $image->setDestination(APPLICATION_PATH. "/../data/images"); //!!!!
    $extension = $image->getFileName();
    if (!empty($extension))
        {
            $extension = @explode(".", $extension);
            $extension = $extension[count($extension)-1];
            $image->addFilter('Rename', sprintf('logo-%s.'.$extension, uniqid(md5(time()), true)));
        }
    
    $image
        ->addValidator('IsImage', false, $estensioni)//doesn't work on WAMPP/XAMPP/LAMPP
        ->addValidator('Size',array('min' => '10kB', 'max' => '1MB', 'bytestring' => true))//limit to 200k
        ->addValidator('Extension', false, $estensioni)// only allow images to be uploaded
        ->addValidator('ImageSize', false, array(
                'minwidth' => $img_width_min,
                'minheight' => $img_height_min,
                'maxwidth' => $img_width_max,
                'maxheight' => $img_height_max
                )
            )
        ->addValidator('Count', false, 1);// ensure that only 1 file is uploaded
    // set the enctype attribute for the form so it can upload files
    $this->setAttrib('enctype', 'multipart/form-data');
    

    然后当您在控制器中提交表单时:

    if ($this->_request->isPost() && $form->isValid($_POST)) {
                $data = $form->getValues();//also transfers the file
    ....
    

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-29
        • 1970-01-01
        相关资源
        最近更新 更多