Home > Article > Backend Development > Which system array does PHP use to receive uploaded file information?
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 //上传文件的大小 ) );
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>
In this form, "file" is the name of the form element.
In $_FILES, the specific information stored includes:
The following is the meaning of each field in $_FILES:
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.
The MIME type of the uploaded file, that is, the content type of the file, such as text/plain, application/octet-stream, etc.
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 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扩展停止文件上传。
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>
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); } ?>
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!