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. 1. introduction whether it is managing videos on YouTube, sharing PowerPoint presentations on SlideShare, or using the open-source e-commerce platform Magento update online "> <LINKhref =" http:
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.
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.
What file wocould you like to upload?
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.
- This form element will create a form mechanism that allows users to search for files to be uploaded in the local file system. When the submit button is pressed, the file will be uploaded and sent to the script marked by the form action (upload. php in this example) for further processing.
As described above, the file will be uploaded to the script identified by the action attribute of the form. However, data will be lost unless the script actually processes the file. In the next section, we will introduce how to use the Zend_File_Transfer component of the Zend Framework to process uploaded files.