How to use files in php
How to use files in php: 1. "$_FILES['myFile']['name']" displays the original name of the client file; 2. "$_FILES['myFile']['type ']" MIME type of the file; 3. The size of the uploaded file in "$_FILES['myFile']['size']"; 4. The temporary file stored in "$_FILES['myFile']['tmp_name']" How to use names, etc.
Operating system for this tutorial: Windows 10 system, PHP version 8.1.3, Dell G3 computer.
Common $_FILES system function usage in the PHP programming language are:
$_FILES['myFile']['name'] Displays the original name of the client file.
$_FILES['myFile']['type'] The MIME type of the file, such as "image/gif".
$_FILES['myFile']['size'] The size of the uploaded file, in bytes.
$_FILES['myFile']['tmp_name'] The name of the temporary file stored, usually the system default.
$_FILES['myFile']['error'] The error code related to file upload.
The following are the meanings of different codes:
0; The file was uploaded successfully.
1; The file size exceeds the size set by the system in php.ini.
2; The file size is exceeded, the value specified by the MAX_FILE_SIZE option.
3; Only part of the file was uploaded.
4; No files were uploaded.
5; The uploaded file size is 0.
Example:
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="submit" /> </form> </body> </html
php processing code
This article will briefly introduce the Friends who need to know about various parameters of $_FILES, an important global variable when uploading files and images in PHP, can refer to it.
Processing of file upload form
The code is as follows:
<form action="post.php" enctype="multipart/form-data" method="post"> <input name="MAX_FILE_SIZE" type="hidden" value="1000" /> <input name="upfile" type="file" /> <input type="submit" value="上传文件" /> </form>
Usage of variables of files
$_FILES super Global variables are very special. They are the only two-dimensional arrays in the predefined super global array. Its role is to store various information related to uploaded files, which is crucial for files uploaded to the server through PHP scripts. There are a total of 5 items in this function:
1, $_FILES["userfile"]["error"]
$_FILES["userfile"]["error"] The array value provides Important information about the results of your upload attempt. There are 5 different return values in total, one indicating a successful result and the other 4 indicating specific errors that occurred during the attempt. The names and names of the return values will be introduced later.
2. The $_FILES["userfile"]["name"]
$_FILES["userfile"]["name"] variable specifies the file declared on the client machine The original name, including extensions. So if you browser a file called vacation.jpg and upload it via a form, the value of this variable will be vacation.png.
3. The $_FILES["userfile"]["size"]
$_FILES["userfile"]["size"] variable specifies the size of the file uploaded from the client. The unit is bytes. Therefore, in the example of the vacation.jpg file, this function may be assigned a value of 5253, which is approximately 5kb.
4, $_FILES["userfile"]["tmp_name"]
$_FILES The ["userfile"]["tmp_name"] variable specifies the temporary name given to the file after uploading to the server. This is the filename specified when stored in the temporary directory (specified by the PHP directive upload_tmp_dir).
5. The $_FILES["userfile"]["type"]
$_FILES["userfile"]["type"] variable specifies the mime type of the file uploaded from the client. Therefore, in the example of the vacation.jpg file, this variable would be assigned the value image/jpeg. If the upload is a PDF, the value is application/pdf. Because this variable can sometimes give unexpected results, it should be verified explicitly in the script.
$_FILES['myFile']['error'] Error code related to the file upload. ['error'] was added in PHP 4.2.0. The following is its description: (They became constants after PHP3.0)
UPLOAD_ERR_OK value: 0; No error occurred and 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.
UPLOAD_ERR_FORM_SIZE value: 2; The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
UPLOAD_ERR_PARTIAL value: 3; Only part of the file was uploaded.
UPLOAD_ERR_NO_FILE value: 4; No file was uploaded. Value: 5; The uploaded file size is 0.
Let’s look at a complete example
Simple file upload
##<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="submit" /> </form> </body> </html> php代码 0) { echo "error: " . $_files["file"]["error"] . "
"; } else { echo "upload: " . $_files["file"]["name"] . "
"; echo "type: " . $_files["file"]["type"] . "
"; echo "size: " . ($_files["file"]["size"] / 1024) . " kb
"; echo "stored in: " . $_files["file"]["tmp_name"]; } } else { echo "invalid file"; } ?>
The above is the detailed content of How to use files in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
