- Zend 框架教程
- Zend 框架 - 主页
- Zend 框架 - 简介
- Zend 框架 - 安装
- 骨架应用
- Zend 框架 - MVC 架构
- Zend 框架 - 概念
- Zend 框架 - 服务管理器
- Zend 框架 - 事件管理器
- Zend 框架 - 模块系统
- 应用结构
- Zend 框架 - 创建模块
- Zend 框架 - 控制器
- Zend 框架 - 路由
- Zend 框架 - 视图层
- Zend 框架 - 布局
- 模型和数据库
- 不同的数据库
- 表格和验证
- Zend 框架 - 文件上传
- Zend 框架 - Ajax
- Cookie 管理
- 会话管理
- Zend 框架 - 身份验证
- 电子邮件管理
- Zend 框架 - 单元测试
- Zend 框架 - 错误处理
- Zend 框架 - 工作示例
- Zend 框架有用的资源
- Zend 框架 - 快速指南
- Zend 框架 - 有用的资源
- Zend 框架 - 讨论
Zend 框架 - 文件上传
文件上传是表单编程的主要概念之一。Zend 框架提供了通过zend-form和zend-inputfilter组件上传文件的所有必需项。
文件输入类
zend-inputfilter 组件提供 Zend\InputFilter\FileInput 类来处理 html 文件输入元素 – <input type = 'file' />。FileInput与其他输入过滤器类似,但有一些例外。它们如下 -
由于 PHP 将上传的文件详细信息保存在$_FILES全局数组中,因此 FileInput 仅通过 $_FILES 收集上传的文件信息。
在 FileInput 类处理数据之前需要完成验证。它与其他输入过滤器的Behave相反。
Zend\Validator\File\UploadFile 是要使用的默认验证器。UploadFile验证文件输入详细信息。
要在表单中添加文件上传类型,我们需要使用输入类型File。部分代码如下 -
$form->add(array(
'name' => 'imagepath',
'type' => 'File',
'options' => array('label' => 'Picture',),
));
文件上传中使用的另一个类是 Zend\Filter\File\RenameUpload。RenameUpload用于将上传的文件移动到我们想要的位置。使用文件过滤器的部分类如下 -
$file = new FileInput('imagepath');
$file->getValidatorChain()->attach(new UploadFile());
$file->getFilterChain()->attach(
new RenameUpload([
'target' => './public/tmpuploads/file',
'randomize' => true,
'use_upload_extension' => true
]));
$inputFilter->add($file);
这里, RenameUpload的选项如下 -
target - 上传文件的目标路径。
randomize - 添加随机字符串以防止重复上传的文件。
use_upload_extension - 将上传文件的文件扩展名附加到目标。
文件上传 - 工作示例
让我们修改教程模块并添加图片上传功能。
修改数据库表
让我们通过执行以下 SQL 命令将imagepath列添加到 book 表中 -
ALTER TABLE `book` ADD `imagepath` VARCHAR(255) NOT NULL AFTER 'imagepath';
更新 BookForm.php
添加文件输入元素以上传书籍表单中的图片 - myapp/module/Tutorial/src/Model/BookForm.php。
在BookForm 类的__constructmethod中包含以下代码。
$this->add(array(
'name' => 'imagepath',
'type' => 'File',
'options' => array ('label' => 'Picture',),
));
更新Book.php
在 Book 类中进行以下更改 – myapp/module/Tutorial/src/Model/Book.php。
为图片添加一个新的属性imagepath 。
public $imagepath;
更新getInputFilter方法,如下所示 -
为文件输入元素添加FileInput过滤器。
设置UploadFile验证以验证文件输入元素。
配置RenameUpload将上传的文件移动到正确的目的地。
部分代码清单如下 -
$file = new FileInput('imagepath');
$file->getValidatorChain()->attach(new UploadFile());
$file->getFilterChain()->attach(
new RenameUpload([
'target' => './public/tmpuploads/file',
'randomize' => true, 'use_upload_extension' => true
]));
$inputFilter->add($file);
更新ExchangeArray方法以包含imagepath属性。图像路径可以来自表单或数据库。如果图像路径来自表单,则格式将是具有以下规范的数组 -
array(1) {
["imagepath"] => array(5) {
["name"] => string "myimage.png"
["type"] => string "image/png"
["tmp_name"] => string
"public/tmpuploads/file_<random_string>.<image_ext>"
["error"] => int <error_number>
["size"] => int <size>
}
}
如果图像路径来自数据库,它将是一个简单的字符串。解析图像路径的部分代码清单如下 -
if(!empty($data['imagepath'])) {
if(is_array($data['imagepath'])) {
$this->imagepath = str_replace("./public", "", $data['imagepath']['tmp_name']);
} else {
$this->imagepath = $data['imagepath'];
}
} else {
$data['imagepath'] = null;
}
Book模型的完整列表如下 -
<?php
namespace Tutorial\Model;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\Filter\File\RenameUpload;
use Zend\Validator\File\UploadFile;
use Zend\InputFilter\FileInput;
use Zend\InputFilter\InputFilter;
class Book implements InputFilterAwareInterface {
public $id;
public $author;
public $title;
public $imagepath;
protected $inputFilter;
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception("Not used");
}
public function getInputFilter() {
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'author',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'title',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$file = new FileInput('imagepath');
$file->getValidatorChain()->attach(new UploadFile());
$file->getFilterChain()->attach(
new RenameUpload([
'target' => './public/tmpuploads/file',
'randomize' => true,
'use_upload_extension' => true
]));
$inputFilter->add($file);
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
public function exchangeArray($data) {
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->author = (!empty($data['author'])) ? $data['author'] : null;
$this->title = (!empty($data['title'])) ? $data['title'] : null;
if(!empty($data['imagepath'])) {
if(is_array($data['imagepath'])) {
$this->imagepath = str_replace("./public", "",
$data['imagepath']['tmp_name']);
} else {
$this->imagepath = $data['imagepath'];
}
} else {
$data['imagepath'] = null;
}
}
}
更新 BookTable.php
我们更新了BookForm和Book 模型。现在,我们更新BookTable并修改saveBook方法。这足以将图像路径条目包含在数据数组$data中。
部分代码清单如下 -
$data = array('author' => $book->author, 'title' => $book->title,
'imagepath' => $book->imagepath
);
BookTable类的完整代码清单如下:
<?php
namespace Tutorial\Model;
use Zend\Db\TableGateway\TableGatewayInterface;
class BookTable {
protected $tableGateway;
public function __construct(TableGatewayInterface $tableGateway) {
$this->tableGateway = $tableGateway;
}
public function fetchAll() {
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getBook($id) {
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
public function saveBook(Book $book) {
$data = array (
'author' => $book->author,
'title' => $book->title,
'imagepath' => $book->imagepath
);
$id = (int) $book->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getBook($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Book id does not exist');
}
}
}
}
更新TutorialController.php中的addAction :文件上传信息将在$_FILES全局数组中可用,并且可以使用Request的getFiles()方法来访问它。因此,合并发布的数据和文件上传信息,如下所示。
$post = array_merge_recursive( $request->getPost()->toArray(), $request->getFiles()->toArray() );
addAction()方法的完整列表如下 -
public function addAction() {
$form = new BookForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if ($request->isPost()) {
$book = new Book();
$form->setInputFilter($book->getInputFilter());
$post = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$form->setData($post);
if ($form->isValid()) {
$book->exchangeArray($form->getData());
$this->bookTable->saveBook($book);
// Redirect to list of Tutorial
return $this->redirect()->toRoute('tutorial');
}
}
return array('form' => $form);
}
更新 add.phtml 的视图
最后,更改“add.phtml”并包含图像路径文件输入元素,如下所示 -
echo $this->formRow($form->get('imagepath'))."<br>";
完整列表如下 -
<?php
$title = 'Add new Book';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
if(!empty($form)) {
$form->setAttribute('action', $this->url('tutorial', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('author'))."<br>";
echo $this->formRow($form->get('title'))."<br>";
echo $this->formRow($form->get('imagepath'))."<br>";
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
}
运行应用程序
最后,在http://localhost:8080/tutorial/add运行应用程序并添加新记录。
结果将如以下屏幕截图所示 -
表格页
索引页
