0

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

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

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

4

3 回答 3

2

通知:

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...
    }
}
于 2011-10-12T13:56:43.090 回答
1

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

$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
....
于 2011-10-12T13:56:19.897 回答
0

以下是一些可以帮助您的链接。

Zend 文档

关于SO的相同问题

逐步教程

另一个有用的链接

于 2011-10-12T13:43:26.570 回答