Home > Backend Development > PHP Problem > How to use php $_files

How to use php $_files

青灯夜游
Release: 2023-03-11 07:06:01
Original
2703 people have browsed it

In php, "$_files" is a predefined array used to obtain information related to files uploaded through the POST method, including the original name of the file, the MIME type of the file, the size of the uploaded file, The temporary file name stored on the server after the file is uploaded, and the error code related to the file upload.

How to use php $_files

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP $_FILES is a predefined array used to obtain information about files uploaded through the POST method. If uploading a single file, $_FILES is a two-dimensional array; if uploading multiple files, $_FILES is a three-dimensional array.

The contents of the array come from the following sample form. We assume that the name of the file upload field is userfile as shown in the example below. The name can be whatever you want.

  • $_FILES['userfile']['name']

    The original name of the client machine file.

  • $_FILES['userfile']['type']

    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.

  • $_FILES['userfile']['size']

    The size of the uploaded file, in bytes.

  • $_FILES['userfile']['tmp_name']

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

  • $_FILES['userfile']['error']

    The error code related to the file upload.

Example:

Create a file.html demonstration upload file, the code is as follows:

<html>
<head></head>
<body></body>
<form enctype="multipart/form-data" action="file.php" method="POST">
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>
</html>
Copy after login

Create a new user For the PHP file file.php that receives file information, the code is as follows:

<?php
echo "<pre class="brush:php;toolbar:false">";
print_r($_FILES);
?>
Copy after login

After selecting the file on the file.html page, click the Send File button, and the following information will be output on the page:

Array
(
    [userfile] => Array
    (
        [name] => Screen Shot 2020-05-12 at 18.13.24.png
        [type] => image/png
        [tmp_name] => /private/var/tmp/phplVHp3W
        [error] => 0
        [size] => 344925
    )
)
Copy after login

Recommended learning: "PHP video tutorial"

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:
php
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