Home >Daily Programming >PHP Knowledge >Detailed explanation of PHP file upload method and information analysis [with video]
This article mainly introduces the PHP file upload method and its information analysis in detail.
Using PHP to implement the file upload function is relatively common in PHP project development, but it may be difficult for some novices. Below we will explain it in detail through specific code examples.
First create an HTML form for file upload.
The code example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> 选择文件进行上传: <input type="file" name="file"> <input type="submit" value="上传"> </form> </body> </html>
The effect is as shown:
#In the above code, we set the file type for the input button, and set the uploaded file name to file. When we click to select a file or image, the form data will be sent to upload.php, and then related operations will be performed on the uploaded file.
Then in the upload.php file, we will define a method to analyze the relevant information of the uploaded file.
The specific code example is as follows:
<?php $fileInfo = $_FILES['file']; var_dump($_FILES['file']); function upload_file($fileInfo, $upload = "./upload", $imagesExt = ['gif', 'png', 'jpg']) { if ($fileInfo['error'] === 0) { $ext = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION)); if (!in_array($ext, $imagesExt)) { return "文件非法类型"; } if (!is_dir($upload)) { mkdir($upload, 0777, true); } $fileName = md5(uniqid(microtime(true), true)) . "." . $ext; $destName = $upload . "/" . $fileName; if (!move_uploaded_file($fileInfo['tmp_name'], $destName)) { return "文件上传失败!"; } return "文件上传成功!"; } else { switch ($fileInfo['error']) { case 1: echo '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值'; break; case 2: echo '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值'; break; case 3: echo '文件只有部分被上传'; break; case 4: echo '没有文件被上传'; break; case 6: echo '找不到临时文件夹'; break; case 7: echo '文件写入失败'; break; } } } var_dump(upload_file($fileInfo));
In the above PHP code, a variable $fileInfo is defined to receive the data uploaded from the form, and then use the var_dump function to print out the information of the uploaded file file and display it in an array.
Then when there is no way to judge the file, the effect of accessing it directly through the browser is as follows:
## Tip : error represents the error message. The error number here is 0, which means that the uploaded file does not have any errors. But in the PHP manual, the error number is more than 0.
The following is a brief summary of the error number situations in table form
The value is 2 | The value is 3 | The value is 4 | The value is 6 | When the value is 7 | ||
The uploaded file exceeds the value of the upload_max_filesize option in php.ini. | The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. | Only part of the file was uploaded. | No files were uploaded | The temporary folder cannot be found. Introduced in PHP 4.3.10 and PHP 5.0.3. | File writing failed. Introduced in PHP 5.1.0. |
Then use if else and switch judgment statements to judge the uploaded files one by one.
The method here is based on the type of file, the existence of the upload folder, and the error message. Finally, after we upload a picture, the final effect displayed is as follows: ##Note:md5() in the above code The function is not used for encryption, but for encoding. The above article is about the specific method of uploading PHP files. I hope it will be helpful to friends in need! The corresponding
Video Tutorialis [PHP File Upload Information Analysis and Encapsulation Upload Method] for your reference and study!
The above is the detailed content of Detailed explanation of PHP file upload method and information analysis [with video]. For more information, please follow other related articles on the PHP Chinese website!