Zend Framework:图片上传

Eri*_*rik 12 php zend-framework file-upload

我想使用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"
Run Code Online (Sandbox Code Playgroud)

的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;
  }
}
?>
Run Code Online (Sandbox Code Playgroud)

我的观点只是回显''形式'变量.

lo_*_*fye 19

首先,将它放在脚本的开头:

error_reporting(E_ALL); //这应显示所有php错误

我认为表单中缺少错误消息,因为您在显示之前重新填充表单.我认为这会消除任何错误消息.要解决此问题,请删除此部分:

else
{
   $f->populate($data);
}
Run Code Online (Sandbox Code Playgroud)

要在表单中显示上传的图像,只需在视图模板中添加div,如下所示:

<div style="float:right"><?=$this->image?></div>
Run Code Online (Sandbox Code Playgroud)

如果上传的图像正常,则使用img标签填充$ view-> image.

至于自动重命名,不,它不是内置的,但它很容易.我会告诉你如何下面.以下是我处理图片上传的方法:

$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', false, 10240000);            // limit to 10 meg
$image->addValidator('Extension', false, '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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)