2011-10-12 67 views
0

我一直在使用普通文件上传元素上传文件并验证它们。但是最近,我发现实现一个Zend_file_tranfer可以很好地控制这个文件。如何使用Zend_File_Transfer?

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

谁能给我用zend_File_tranfers,在这两个Zend_Form和依靠Zend_Controller_Action

+0

我希望我问一个问题可以理解。基本上我想从初学者的角度来看,使用Zend_File_Transfer来控制文件上传。 – mrN

回答

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