Home > Backend Development > PHP Tutorial > How to use php $_FILES

How to use php $_FILES

怪我咯
Release: 2023-03-13 18:50:02
Original
1826 people have browsed it

This article mainly introduces the usage of $_FILES in PHP and precautions. Friends who need it can come and refer to it. I hope it will be helpful to everyone

$ _FILES: Variables submitted to the script via HTTP POST File upload, similar to the old array $HTTP_POST_FILES array (still valid, but deprecated) For details, see POST method upload

## The contents of the #$_FILES array are as follows:

$_FILES['myFile']['name'] The original name of the client file

$_FILES[' myFile']['type'] The MIME type of the file, which requires the browser to provide support for this information, such as "image/gif"

$_FILES['myFile']['size'] The uploaded file Size, in bytes

$_FILES['myFile']['tmp_name'] The temporary file name stored on the server after the file is uploaded. It is generally the system default and can be specified in upload_tmp_dir of php.ini , but setting it with the putenv() function does not work

$_FILES['myFile']['error'] The error code related to the file upload, ['error'] is in PHP 4.2. Added in version 0, the following is its description: (They became

constants after PHP3.0)

UPLOAD_ERR_OK value: 0; No error occurred, the file was uploaded successfully

UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the value limited by the upload_max_

filesize option in php.ini                                                The value specified by the option              

UPLOAD_ERR_PARTIAL Value: 3; Only part of the file was uploaded                                                                                                                                   #UPLOAD_ERR_NO_FILE Value: 4;

#Note:

1. After the file is uploaded, it is stored in the temporary directory by default. At this time, it must be Delete from the temporary directory or move it to another location, if not, it will be deleted. That is to say, 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, you need to use PHP's copy() function to copy it to another location before deleting it. At this time, the file upload process is completed.

2. Before PHP 4.1.0, the name of this array was $HTTP_POST_FILES, which is not an automatic global variable like $_FILES. PHP 3 does not support the $HTTP_POST_FILES array. 3.

When uploading a file using form, be sure to add the

attribute content enctype="multipart/form-data", otherwise use $_FILES[filename] to obtain it An exception will be reported when reading file information.

<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
	<label for="file">文件名:</label>
	<input type="file" name="file" id="file"><br>
	<input type="submit" name="submit" value="提交">
</form>

</body>
</html>
Copy after login

Save the above code into the form.html file.

Some notes about the above HTML form are listed below:The enctype attribute of the

tag specifies which content type to use when submitting the form. Use "multipart/form-data" when your form requires binary data, such as file content.

The type="file" attribute of the tag specifies that the input should be processed as a file. For example, when previewing in a browser, you'll see a browse button next to the input box.

Note: Allowing users to upload files is a huge security risk. Please allow only trusted users to perform file upload operations.

The above is the detailed content of How to use php $_FILES. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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