Home Backend Development PHP Problem Which system array does PHP use to receive uploaded file information?

Which system array does PHP use to receive uploaded file information?

May 06, 2023 am 10:43 AM

The system array used by PHP to receive uploaded file information is $_FILES.

In PHP, we often need to handle file uploads, such as uploading pictures, videos and other files. In order to handle relevant information and data during the upload process, PHP provides a special system array $_FILES to store uploaded file information.

$_FILES variable is a two-dimensional associative array with the following structure:

$_FILES = array(
    'file' => array(
        'name' => 'filename.txt',             //文件名
        'type' => 'text/plain',               //文件MIME类型
        'tmp_name' => '/tmp/php/php1h4jCk',   //上传的临时文件名
        'error' => 0,                         //上传的错误代码
        'size' => 123                           //上传文件的大小
   )
);
Copy after login

Among them, "file" in the array is the name of the form element, such as the following HTML code:

<form enctype="multipart/form-data" method="POST">
    <input type="file" name="file"/>
</form>
Copy after login

In this form, "file" is the name of the form element.

In $_FILES, the specific information stored includes:

  • name: Uploaded file name.
  • type: MIME type of uploaded file.
  • tmp_name: The name of the uploaded temporary file, stored in the temporary directory on the server side.
  • error: The error code of the uploaded file. If it is 0, it means the upload is successful.
  • size: The size of the uploaded file.

The following is the meaning of each field in $_FILES:

  • name

The original file name of the uploaded file, excluding the file path. If the file name is modified when a file is uploaded, this variable stores the new file name.

  • #type

The MIME type of the uploaded file, that is, the content type of the file, such as text/plain, application/octet-stream, etc.

  • tmp_name

The temporary file name of the uploaded file, and the path to store the temporary file. This path is the temporary directory on the server side.

  • error

Error code for uploading files. If the upload is successful, this value is 0. Other possible values ​​include:

- UPLOAD_ERR_OK:文件上传成功。
- UPLOAD_ERR_INI_SIZE:上传的文件超过了php.ini 中 upload_max_filesize 选项限制的值。
- UPLOAD_ERR_FORM_SIZE:上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
- UPLOAD_ERR_PARTIAL:文件只有部分被上传。
- UPLOAD_ERR_NO_FILE:没有文件被上传。
- UPLOAD_ERR_CANT_WRITE:写入磁盘失败。
- UPLOAD_ERR_EXTENSION:PHP扩展停止文件上传。
Copy after login
  • size

The size of the uploaded file, in bytes.

Example of using $_FILES variable:

If we want to upload an image in a form, we can use the following HTML code:

<form enctype="multipart/form-data" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    <input type="file" name="image" />
    <input type="submit" name="submit" value="Upload Image" />
</form>
Copy after login

On the server side, we can use The following PHP code handles uploaded files:

<?php
if(isset($_POST['submit']))
{
    $file = $_FILES['image'];
    $filename = $file['name'];
    $filetmp = $file['tmp_name'];
    $filesize = $file['size'];
    $filetype = $file['type'];

    // TODO: 处理上传文件

    move_uploaded_file($filetmp, "uploads/" . $filename);
}
?>
Copy after login

When processing uploaded files, we first check whether the submitted form contains the "submit" field. This is to avoid processing uploaded files without submitting the form. mistake.

We first get the file information from the $_FILES variable, and define the variables $filename, $filetmp, $filesize, and $filetype to store the file name, temporary file name of the uploaded file, file size, and MIME type respectively.

Next, we can use the PHP function move_uploaded_file() function to move the uploaded file from the temporary directory to the specified directory where we store the file, such as the "uploads/" directory in the above example.

Summary:

$_FILES is the key variable for processing file uploads in PHP. It contains various information about uploaded files, including file name, MIME type, upload temporary file name, size, etc. wait. When using $_FILES, we need to pay attention to the timing of form submission, the logic of processing uploaded files, and the storage method of uploaded files.

The above is the detailed content of Which system array does PHP use to receive uploaded file information?. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. Mar 25, 2025 am 10:37 AM

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

PHP Secure File Uploads: Preventing file-related vulnerabilities. PHP Secure File Uploads: Preventing file-related vulnerabilities. Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Encryption: Symmetric vs. asymmetric encryption. PHP Encryption: Symmetric vs. asymmetric encryption. Mar 25, 2025 pm 03:12 PM

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

PHP Authentication & Authorization: Secure implementation. PHP Authentication & Authorization: Secure implementation. Mar 25, 2025 pm 03:06 PM

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

PHP API Rate Limiting: Implementation strategies. PHP API Rate Limiting: Implementation strategies. Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP CSRF Protection: How to prevent CSRF attacks. PHP CSRF Protection: How to prevent CSRF attacks. Mar 25, 2025 pm 03:05 PM

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

PHP Input Validation: Best practices. PHP Input Validation: Best practices. Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

See all articles