我想使用Zend Framework版本1.9.6上传图像.上传本身工作正常,但我也想要其他几件事情,而且我完全卡住了.
>无法上传图像的错误消息将不会显示.
>如果用户没有输入所有必填字段,但已上传图像,那么我想以我的形式显示上传的图像.作为图像或作为图像的链接.只是一些形式的反馈给用户.
>我想使用Zend_ Validate_ File_ IsImage.但它似乎没有任何作用.
>最后;有没有一些自动重命名功能?
所有的想法和建议是非常欢迎的.我一直在努力两天.
这些是简化的代码片段:
myform.ini
method = "post" elements.title.type = "text" elements.title.options.label = "Title" elements.title.options.attribs.size = 40 elements.title.options.required = true elements.image.type = "file" elements.image.options.label = "Image" elements.image.options.validators.isimage.validator = "IsImage" elements.submit.type = "submit" elements.submit.options.label = "Save"
的TestController
<?PHP class Admin_TestController extends Zend_Controller_Action { public function testAction () { $config = new Zend_Config_Ini(MY_SECRET_PATH . 'myform.ini'); $f = new Zend_Form($config); if ($this->_request->isPost()) { $data = $this->_request->getPost(); $imageElement = $f->getElement('image'); $imageElement->receive(); //$imageElement->getValue(); if ($f->isValid($data)) { //save data $this->_redirect('/admin'); } else { $f->populate($data); } } $this->view->form = $f; } } ?>
我的观点只是echo的’form’变量.
首先,把它放在脚本的开头:
error_reporting(E_ALL); //这应该显示所有的PHP错误
我认为窗体中缺少错误消息,因为您在显示窗体之前重新填充表单.我认为会擦除任何错误信息.要解决这个问题,请删除此部分:
else { $f->populate($data); }
要在表单中显示上传的图像,只需将div添加到视图模板,如下所示:
<div style="float:right"><?=$this->image?></div>
如果图像上传ok,然后使用img标签填充$view->图像.
对于自动重命名,不,它不是内置的,但它很容易.我会告诉你如何下面.
这是我如何处理我的图像上传:
$form = new Zend_Form(); $form->setEnctype(Zend_Form::ENCTYPE_MULTIPART); $image = new Zend_Form_Element_File('image'); $image->setLabel('Upload an image:') ->setDestination($config->paths->upload) ->setrequired(true) ->setMaxFileSize(10240000) // limits the filesize on the client side ->setDescription('Click Browse and click on the image file you would like to upload'); $image->addValidator('Count',false,1); // ensure only 1 file $image->addValidator('Size',10240000); // limit to 10 meg $image->addValidator('Extension','jpg,jpeg,png,gif');// only JPEG,PNG,and GIFs $form->addElement($image); $this->view->form = $form; if($this->getRequest()->isPost()) { if(!$form->isValid($this->getRequest()->getParams())) { return $this->render('add'); } if(!$form->image->receive()) { $this->view->message = '<div class="popup-warning">Errors Receiving File.</div>'; return $this->render('add'); } if($form->image->isUploaded()) { $values = $form->getValues(); $source = $form->image->getFileName(); //to re-name the image,all you need to do is save it with a new name,instead of the name they uploaded it with. Normally,I use the primary key of the database row where I'm storing the name of the image. For example,if it's an image of Person 1,I call it 1.jpg. The important thing is that you make sure the image name will be unique in whatever directory you save it to. $new_image_name = 'someNameYouInvent'; //save image to database and filesystem here $image_saved = move_uploaded_file($source,'/www/yoursite/images/'.$new_image_name); if($image_saved) { $this->view->image = '<img src="/images/'.$new_image_name.'" />'; $form->reset();//only do this if it saved ok and you want to re-display the fresh empty form } } }