Home > Article > Backend Development > 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"] . "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!