Home Backend Development PHP Problem How to use php $_files

How to use php $_files

Jul 01, 2021 pm 06:19 PM
php

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>";
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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles