本文将向读者详细介绍如何使用流行的Zend框架来创建我们自己的文件上传机制,该机制能够轻而易举地接收、确认和处理要上传的文件。
一、简介
无论是管理YouTube上的视频、在SlideShare上共享PowerPoint演示文稿,还是使用开源电子商务平台Magento更新在线商店中的商品图像,我们都有用到基于web的文件上传功能。但是,这个功能到底是如何工作的呢?文件是如何从本地机器传送到远程服务器的?本文将向读者详细介绍如何使用流行的Zend框架来创建我们自己的文件上传机制,该机制能够轻而易举地接收、确认和处理要上传的文件。
二、配置处理文件上载的PHP
PHP本身能够通过Web表单来完成文件上载,不过无论使用标准PHP代码还是使用Zend框架来管理文件上传,都有必要花一些时间来检测直接影响PHP的有关功能的配置伪指令,这些指令包括:
- file_uploads:这个伪指令启用PHP的文件上载功能。在默认情形下,这个伪指令会被启用。
- upload_max_filesize:这个伪指令定义了要上载的文件的最大尺寸。在默认情形下,这个伪指令被设置为2M。
- upload_tmp_dir:这个伪指令定义了PHP临时存储要上传的文件的目录,文件在传入最终目的地之前将临时存放于此,该目录是由开发人员指定的。在默认情形下,这个伪指令不会赋值,这意味着PHP将使用系统的默认值,举例来说,许多linux发行版中的临时目录为/tmp。
- post_max_size:设定POST 数据所允许的最大大小。php默认的post_max_size 为2M。
- max_execution_time:虽然与文件上载的关系不是非常密切,但是这个伪指令在PHP的文件上载功能中却扮演了一个重要角色,因为它定义了PHP脚本的执行时间。对于尺寸特别大的文件来说,可能需要很长的时间才能传输到文件服务器,所以可以考虑将这个伪指令的默认值即30秒改为60乃至90秒。
三、创建文件上载表单
下面我们将创建一个Web表单示例,它能够用来浏览本地计算机的文件系统以及确定要上传的文件。我们将尽力使该示例保持简单,创建的文件上载表单的外观如图1所示。
图1 一个简单的文件上载表单
这个表单的创建方式跟我们之前创建的其他表单大同小异,只是有一些轻微的区别。用于创建这个表单的HTML代码如清单1所示。按照Zend框架的惯例,我们将这个表单放入名为upload.phtml的视图中,它是名为admin的控制器中动作upload的一部分。
form enctype="multipart/form-data" method="post" action="/admin/upload">
p>
What file would you like to upload?br />
input type="file" name="video-upload" size="40" />
p>
p>
input type="submit" name="submit" class="submit" value="Upload Video" />
p>
form>
清单1 文件上载表单的HTML代码
在这个表单中,有两处代码需要格外注意:
- enctype="multipart/form-data":当我们使用Web表单发送大量的二进制数据的时候,就应该使用这个表单属性。因为诸如电子表格以及视频等文件中含有大量的二进制数据,所以创建文件上载表单时应该包含这个属性。
-
input type="file" name="video-upload

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.