Home > Article > Backend Development > Clever Use of POST Method to Upload Files in PHP_PHP Tutorial
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.
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.