This article will introduce you in detail how to use the popular Zend framework to create our own file upload mechanism, which can easily receive, confirm, and process the files to be uploaded.
I. Introduction
Whether it's managing videos on YouTube, sharing PowerPoint presentations on SlideShare, or using the open-source e-commerce platform Magento to update product images in online stores, we all use web-based file upload. But how does this function work? How does a file be transmitted from a local machine to a remote server? This article will introduce you in detail how to use the popular Zend framework to create our own file upload mechanism, which can easily receive, confirm, and process the files to be uploaded.
Ii. Configure PHP for processing file uploads
PHP itself can upload files through Web forms, but whether it uses standard PHP code or the Zend framework to manage file uploads, it is necessary to take some time to detect the configuration pseudoinstructions that directly affect PHP functions. These Commands include:
- File_uploads: This pseudocommand enables the PHP file upload function. By default, this pseudo command is enabled.
- Upload_max_filesize: this directive defines the maximum size of the file to be uploaded. By default, this pseudo command is set to 2 MB.
- Upload_tmp_dir: This pseudo command defines the directory for PHP to temporarily store the files to be uploaded. The files will be temporarily stored in this directory before being passed into the final destination, which is specified by developers. By default, this pseudo command is not assigned a value, which means that PHP will use the default value of the system. For example, the temporary directory in many linux releases is/tmp.
- Post_max_size: set the maximum size allowed by POST data. The default post_max_size of php is 2 M.
- Max_execution_time: Although not closely related to file upload, this pseudocommand plays an important role in the PHP file upload function because it defines the execution time of PHP scripts. It may take a long time for an extremely large file to be transmitted to the file server. Therefore, you can change the default value of this pseudocommand to 30 seconds to 60 or 90 seconds.
3. Create a file upload form
Next we will create a Web form example that can be used to browse the file system of the Local Computer and determine the file to be uploaded. We will try our best to keep this example simple, as shown in figure 1.
Figure 1 a simple File Upload form
The method for creating this form is similar to that of other forms we have previously created, but there are some slight differences. The HTML code used to create this form is shown in Listing 1. According to the Zend framework convention, we put this form into the view named upload. phtml, which is part of the action upload in the Controller named admin.
<Form enctype = "multipart/form-data" method = "post" action = "/admin/upload">
<P>
What file wocould 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>
Listing 1 HTML code of the file upload form
There are two codes in this form that require special attention:
- Enctype = "multipart/form-data": This form attribute should be used when a large amount of binary data is sent using a Web form. Because files such as workbooks and videos contain a large amount of binary data, this attribute should be included when a file upload form is created.
- <Input type = "file" name = "video-upload