SoFunction
Updated on 2025-03-10

Analyze the use of $_FILES in PHP and precautions

Analyze the use of $_FILES in PHP and precautions

Updated: July 5, 2013 17:23:11 Author:
$_FILES: A variable submitted to the script via an HTTP POST file upload. Similar to the old array $HTTP_POST_FILES array (still valid, but against use)

The contents of the $_FILES array are as follows:
$_FILES['myFile']['name'] The original name of the client file.
The MIME type of the $_FILES['myFile']['type'] file requires the browser to provide support for this information, such as "image/gif".
$_FILES['myFile']['size'] The size of the uploaded file, in bytes.
$_FILES['myFile']['tmp_name'] The temporary file name stored on the server after the file is uploaded, which is generally the system default. It can be specified in upload_tmp_dir, but setting it with putenv() function does not work.
$_FILES['myFile']['error'] error code related to uploading this file. ['error'] was added in PHP 4.2.0 version.

Here is its description: (They became constants after PHP3.0)
UPLOAD_ERR_OK Value: 0; No error occurred and file upload was successful.
UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the value limit of the upload_max_filesize option in .
UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file size exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
UPLOAD_ERR_PARTIAL Value: 3; Only partial files are uploaded.
UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded. Value: 5; Upload file size is 0.

Note:
1. After the file is uploaded, it is stored in the temporary directory by default. At this time, it must be deleted from the temporary directory or moved to another place. If not, it will be deleted. That is, regardless of whether the upload is successful or not, the files in the temporary directory will definitely be deleted after the script is executed. Therefore, before deleting, you need to use PHP's copy() function to copy it to another location. Only then can the file upload process be considered complete.

2. Prior to PHP 4.1.0, the array had the name of $HTTP_POST_FILES, which was not an automatic global variable like $_FILES. PHP 3 does not support $HTTP_POST_FILES arrays.

3. When uploading files with form, be sure to add the attribute content enctype="multipart/form-data", otherwise an exception will be reported when using $_FILES[filename] to obtain file information.
<form enctype="multipart/form-data" action="URL" method="post">
<input name="myFile" type="file">
<input type="submit" value="upload file">
</form>

  • PHP
  • $ FILES
  • use

Related Articles

  • Summary of PDO operation skills for PHP data object

    This article mainly introduces the PDO operation method of PHP data object, and summarizes and analyzes various common database operation techniques and precautions based on PDO in combination with examples. Friends who need it can refer to it.
    2016-09-09
  • Analysis on the difference between single quotes and double quotes in PHP

    In PHP, we can use single or double quotes to represent strings. However, as developers, we should understand the differences. Single quotes and double quotes are used to define characters, one can parse variables and the other can output the variables directly. At the same time, single quotes and double quotes are better and double quotes are used to character processing.
    2014-08-08
  • Example of simple get verification operation of PHP+Ajax

    This article mainly introduces the simple get verification operation of PHP+Ajax, involving PHP combined with Ajax refresh-free submission verification related operation techniques. Friends who need it can refer to it
    2019-03-03
  • Summary of several common ways of traversing PHP arrays

    This article mainly introduces several common methods of PHP array traversal. It summarizes and analyzes the operation techniques related to PHP array traversal, such as loops, pointers, array functions, etc. in combination with the example form. Friends who need it can refer to it.
    2019-02-02
  • php method to get the current URL url and replace the parameters or URL

    I wrote down the two functions I used in the project today and used them as a backup. Friends who need them can also use them directly. It is recommended that the first function is to obtain, and the subsequent related processing parameters are included.
    2010-06-06
  • Analyze the PHP function in WordPress that controls user login and judges user login

    This article mainly introduces the PHP function in WordPress that controls user login and judges user login. WordPress has been launched for a while now, and the development of multi-user needs is increasing. Friends who need it can refer to it.
    2016-03-03
  • PHP Ajax implements refresh-free attachment upload

    This article mainly introduces the specific code of PHP Ajax to implement the refresh-free attachment upload function, which has certain reference value. Interested friends can refer to it.
    2016-08-08
  • Classes for text operations in php

    Classes for text operations in php...
    2007-03-03
  • The difference between single quotes and double quotes in PHP

    I think many codes sometimes use single quotes or double quotes to implement the content containing strings. In fact, variables in double quotes can be parsed in a simple summary. Single quotes are absolute strings.
    2009-11-11
  • php method to implement httpRequest

    This article mainly introduces the method of implementing httpRequest by php, which involves the skills of php to operate http, and has certain reference value. Friends who need it can refer to it.
    2015-03-03

Latest Comments