Using Zend Framework to Manage File Upload_PHP Tutorial

WBOY
Release: 2016-07-13 17:38:57
Original
1182 people have browsed it

This article will give readers a detailed introduction to how to use the popular Zend framework to create our own file upload mechanism that can easily receive, confirm and process files to be uploaded.

1. Introduction

Whether it is managing videos on YouTube, sharing PowerPoint presentations on SlideShare, or updating product images in an online store using the open source e-commerce platform Magento, we all use the web-based file upload function. . But, how exactly does this feature work? How are files transferred from the local machine to the remote server? This article will give readers a detailed introduction to how to use the popular Zend framework to create our own file upload mechanism, The mechanism makes it easy to receive, confirm and process files for upload.

 2. Configure PHP to handle file upload

PHP itself can complete file uploads through web forms. However, whether you use standard PHP code or use the Zend framework to manage file uploads, it is necessary to spend some time to detect the configuration directives that directly affect the relevant functions of PHP. These instructions Includes:

  • file_uploads: This directive enables PHP's file upload function. By default, this directive is enabled.
  • upload_max_filesize: This directive defines the maximum size of files to be uploaded. By default, this directive is set to 2M.
  • upload_tmp_dir: This directive defines the directory where PHP temporarily stores files to be uploaded. The files will be temporarily stored here before being passed to the final destination. This directory is specified by the developer. By default, this directive will not assign a value, which means that PHP will use the system default value, for example, the temporary directory in many Linux distributions is /tmp.
  • post_max_size: Set the maximum size allowed for POST data. PHP's default post_max_size is 2M.
  • max_execution_time: Although not very closely related to file upload, this directive plays an important role in PHP's file upload function because it defines the execution time of the PHP script. For particularly large files, it may take a long time to be transferred to the file server , so you can consider changing the default value of this directive from 30 seconds to 60 or even 90 seconds.

 3. Create file upload form

Below we will create an example web form that can be used to browse the local computer's file system and determine the files to upload. We'll try to keep this example simple and create a file upload form that looks like Figure 1.


Figure 1 A simple file upload form

The way to create this form is similar to the other forms we created before, with some slight differences. The HTML code used to create this form is shown in Listing 1. Following Zend Framework convention, we put this form into a view named upload.phtml, which is part of the action upload in a controller named admin.

<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>

Listing 1 HTML code for file upload form

In this form, there are two codes that require special attention:

  1. enctype="multipart/form-data": This form attribute should be used when we use web forms to send large amounts of binary data. Because files such as spreadsheets and videos contain large amounts of binary data, you should include this attribute when creating file upload forms.
  2. <input type="file" name="video-upload

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486448.htmlTechArticleThis article will give readers a detailed introduction to how to use the popular Zend framework to create our own file upload mechanism that can Receive, confirm and process files for upload with ease. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template