开发者

How to use Zend_File_Transfer?

开发者 https://www.devze.com 2023-04-12 06:49 出处:网络
I have been using normal file upload element to upload files and validate them. But recently, I 开发者_运维知识库found out that implementing a Zend_file_tranfer gives much control over the file.

I have been using normal file upload element to upload files and validate them. But recently, I 开发者_运维知识库found out that implementing a Zend_file_tranfer gives much control over the file.

I am searched Everywhere in the internet searching for a simple example to get started with it, but none of them show how they are linked to the element. I dont know where to create the object of Zend_File_Transfer, and how to add it to the element? I basically dont know, how to use it.

Can anyone give me a beginners example of using zend_File_tranfers, in both zend_form and Zend_Controller_Action


In form:

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

In controller:

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...
    }
}


When you create your form do something like this in your form:

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

Then when you submit your form in your controller:

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


Here are some links that can help you.

Zend Documentation

Same Question on SO

Step By Step Toturial

Another Useful Link

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号