Как использовать Zend_File_Transfer? - PullRequest
0 голосов
/ 12 октября 2011

Я использовал обычный элемент загрузки файлов, чтобы загружать файлы и проверять их. Но недавно я обнаружил, что реализация Zend_file_tranfer дает большой контроль над файлом.

В Интернете я ищу везде простой пример, чтобы начать с ним работать, но ни один из них не показывает, как они связаны с элементом. Я не знаю, где создать объект Zend_File_Transfer, и как добавить его к элементу? Я в принципе не знаю, как его использовать.

Может кто-нибудь дать мне пример для начинающих использования zend_File_tranfers, как в zend_form, так и в Zend_Controller_Action

Ответы [ 3 ]

2 голосов
/ 12 октября 2011

В форме:

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...
    }
}
1 голос
/ 12 октября 2011

Когда вы создаете форму, сделайте что-то подобное в форме:

$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
....
0 голосов
/ 12 октября 2011
...