我一直使用普通文件上传元素上传文件并验证它们。但最近,我发现实现Zend_file_tranfer可以很好地控制文件。
我在互联网上的任何地方搜索一个简单的例子来开始使用它,但它们都没有显示它们是如何链接到元素的。我不知道在哪里创建Zend_File_Transfer的对象,以及如何将其添加到元素?我基本上不知道,如何使用它。
有没有人能给我一个在zend_form和Zend_Controller_Action中使用zend_File_tranfers的初学者示例
答案 0 :(得分: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...
}
}
答案 1 :(得分: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 :(得分:0)