0

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

mrN
  • 3,734
  • 15
  • 58
  • 82
  • I am hoping I asked a understandable question. Basically I want simple guide from a beginner point of view for using Zend_File_Transfer, to control file uploads. – mrN Oct 12 '11 at 13:33

3 Answers3

2

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...
    }
}
David Weinraub
  • 14,144
  • 4
  • 42
  • 64
kennycoder
  • 56
  • 3
1

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
....
max4ever
  • 11,909
  • 13
  • 77
  • 115
0

Here are some links that can help you.

Zend Documentation

Same Question on SO

Step By Step Toturial

Another Useful Link

Community
  • 1
  • 1
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137