Home > Backend Development > PHP Tutorial > Detailed explanation of $_FILES in PHP super global variables

Detailed explanation of $_FILES in PHP super global variables

藏色散人
Release: 2023-04-08 10:48:01
forward
3950 people have browsed it

Detailed explanation of $_FILES in PHP super global variables

$_FILES - An array of items uploaded to the current script via HTTP POST.

Suppose we upload the file field name='userfile', the $_FILES array includes:

$_FILES['userfile']['name']      //客户端机器文件的原名称。
$_FILES['userfile']['type']     //文件的 MIME 类型,如果浏览器提供此信息的话。一个例子是“image/gif”。不过此 MIME 类型在 PHP 端并不检查,因此不要想当然认为有这个值。
$_FILES['userfile']['size']     //已上传文件的大小,单位为字节。
$_FILES['userfile']['tmp_name']   //文件被上传后在服务端储存的临时文件名。
$_FILES['userfile']['error']      //和该文件上传相关的错误代码。此项目是在 PHP 4.2.0 版本中增加的。
/**
Copy after login

The error code is:

UPLOAD_ERR_OK

The value is 0, no error occurs, and the file is uploaded successfully.

UPLOAD_ERR_INI_SIZE

The value is 1, and the uploaded file exceeds the value limited by the upload_max_filesize option in php.ini.

UPLOAD_ERR_FORM_SIZE

The value is 2, and the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.

eg, add a hidden field in the form: the value unit is bytes

<form enctype="multipart/form-data" action="test.php" method="POST">
  <!-- MAX_FILE_SIZE must precede the file input field -->
  <input type="hidden" name="MAX_FILE_SIZE" value="12" />
  <!-- Name of input element determines name in $_FILES array -->
  Send this file: <input name="userfile" type="file" />
  <input type="submit" value="Send File" />
</form>
Copy after login

         

UPLOAD_ERR_PARTIAL

The value is 3. Only part of the file is uploaded.

UPLOAD_ERR_NO_FILE

The value is 4 and no file was uploaded.

UPLOAD_ERR_NO_TMP_DIR

The value is 6 and the temporary folder cannot be found. Introduced in PHP 4.3.10 and PHP 5.0.3.

UPLOAD_ERR_CANT_WRITE

The value is 7, file writing failed. Introduced in PHP 5.1.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.

Other functions that may be used for file upload:

is_uploaded_file:

is_uploaded_file — 判断文件是否是通过 HTTP POST 上传的
is_uploaded_file ( string $filename ) : bool
Copy after login

Parameters: filename - the file name to be checked.

Return value: TRUE on success, or FALSE on failure.

Returns TRUE if the file given by filename was uploaded via HTTP POST. This can be used to ensure that a malicious user cannot trick a script into accessing otherwise inaccessible files, such as /etc/passwd.

move_uploaded_file:

move_uploaded_file — 将上传的文件移动到新位置
move_uploaded_file ( string $filename , string $destination ) : bool
Copy after login

Parameters: filename The file name of the uploaded file; destination moves the file to this location (absolute path)

This function checks And ensure that the file specified by filename is a legal upload file (that is, uploaded through PHP's HTTP POST upload mechanism). If the file is legal, it is moved to the file specified by destination.

For more related php knowledge, please visit php tutorial!

The above is the detailed content of Detailed explanation of $_FILES in PHP super global variables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
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