gpt4 book ai didi

php - Zend 框架 : image upload

转载 作者:行者123 更新时间:2023-12-03 01:02:59 27 4
gpt4 key购买 nike

我想使用 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"

测试 Controller

<?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;
}
}
?>

我的观点只是回显“form”变量。

最佳答案

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

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

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

else
{
$f->populate($data);
}

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

<div style="float:right"><?=$this->image?></div>

如果图像上传正常,则使用 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
}
}
}

关于php - Zend 框架 : image upload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1868143/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com