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"] . "For IE, the type of jpg file recognized must be pjpeg, and for FireFox, it must be jpeg.
"; } 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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

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.

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.

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

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

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.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools