Home  >  Article  >  Backend Development  >  Clever Use of POST Method to Upload Files in PHP_PHP Tutorial

Clever Use of POST Method to Upload Files in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:26:481073browse

When learning PHP, you may encounter PHP upload problems. Here we will introduce the solutions to PHP upload problems and share them with you here. This feature enables users to upload text and binary files. Using PHP's authentication and file operation functions, you can fully control who is allowed to upload and how to process the files uploaded by PHP after they are uploaded. Note that PHP also supports the PUT method of file upload, which is used by Netscape Composer and the W3C's Amaya client.

Example 1. File upload form

You can create a special form as follows to support file upload:

<ol class="dp-xml">
<li class="alt"><span><span class="comments"><font color="#008200"><!-- The data encoding type, enctype, MUST be specified as below --></font></span><span> </span></span></li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>form</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>enctype</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"multipart/form-data"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>action</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"__URL__"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>method</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"POST"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><span class="comments"><font color="#008200"><!-- MAX_FILE_SIZE must precede the file input field --></font></span><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"hidden"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"MAX_FILE_SIZE"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>value</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"30000"</FONT></SPAN><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></span></font></strong><span> </span>
</li>
<li class="alt">
<span><!-- Name of input element determines name in --</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span><span> </span>
</li>
</ol>

PHP upload POST method upload

This feature allows users to upload text and binary files. Using PHP's authentication and file manipulation functions, you have full control over who is allowed to upload and what to do with files after they are uploaded. PHP can accept files uploaded from any browser that conforms to the RFC-1867 standard (including Netscape Navigator 3 and higher, and patched Microsoft Internet Explorer 3 and higher). Note that PHP also supports the PUT method of file upload, which is used by Netscape Composer and the W3C's Amaya client.

Example 2. File upload form

You can create a special form to support file upload as follows:

<ol class="dp-xml"><li class="alt"><span><span>___FCKpd___0 </span></span></li></ol>

_ in the above example _URL__ should be replaced to point to a real PHP file.

MAX_FILE_SIZE hidden field (unit: bytes) must be placed before the file input field, and its value is the maximum size of the received file. This is a recommendation for browsers, PHP also checks this. This setting can be easily bypassed on the browser side, so don't expect to use this feature to block large files. In fact, the maximum upload file size in PHP settings will not expire. But it is better to add this item to the form, because it can avoid the trouble of users spending time waiting for large files to be uploaded only to find that the file is too large and the upload failed. Note: Make sure the attribute of the file upload form is enctype="multipart/form-data", otherwise the file cannot be uploaded.

The global variable $_FILES exists since PHP 4.1.0 (replaced by $HTTP_POST_FILES in earlier versions). This array contains information about all uploaded files. The contents of the $_FILES array in the above example are as follows. Let's assume that the name of the file upload field is userfile as shown in the example above. The name can be whatever you want.

<ol class="dp-xml"><li class="alt"><span><span>$_FILES['userfile']['name'] </span></span></li></ol>

The original name of the client machine file.

<ol class="dp-xml"><li class="alt"><span><span>$_FILES['userfile']['type'] </span></span></li></ol>

The MIME type of the file, if the browser provides this information. An example is "image/gif". However, this MIME type is not checked on the PHP side, so don't take it for granted.

<ol class="dp-xml"><li class="alt"><span><span>$_FILES['userfile']['size'] </span></span></li></ol>

The size of the uploaded file, in bytes.

<ol class="dp-xml"><li class="alt"><span><span>$_FILES['userfile']['tmp_name'] </span></span></li></ol>

The temporary file name stored on the server after the file is uploaded.

<ol class="dp-xml"><li class="alt"><span><span>$_FILES['userfile']['error'] </span></span></li></ol>

Error code related to the file upload. This project was added in PHP version 4.2.0. After the file is uploaded, it will be stored in the default temporary directory of the server by default, unless upload_tmp_dir in php.ini is set to another path. The default temporary directory on the server side can be reset by changing the environment variable TMPDIR of the PHP running environment, but setting it by running the putenv() function inside the PHP script has no effect. This environment variable can also be used to confirm that other operations are also performed on the uploaded file.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446564.htmlTechArticleWhen learning PHP, you may encounter PHP upload problems. Here we will introduce the solutions to PHP upload problems. Take it out here and share it with everyone. This feature allows users to upload text and...
Statement:
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