Home > Daily Programming > PHP Knowledge > Detailed explanation of PHP file upload method and information analysis [with video]

Detailed explanation of PHP file upload method and information analysis [with video]

藏色散人
Release: 2019-11-02 18:02:10
Original
5467 people have browsed it

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>
Copy after login

The effect is as shown:

Detailed explanation of PHP file upload method and information analysis [with video]

#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[&#39;file&#39;];
var_dump($_FILES[&#39;file&#39;]);

function upload_file($fileInfo, $upload = "./upload", $imagesExt = [&#39;gif&#39;, &#39;png&#39;, &#39;jpg&#39;])
{
    if ($fileInfo[&#39;error&#39;] === 0) {
        $ext = strtolower(pathinfo($fileInfo[&#39;name&#39;], 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[&#39;tmp_name&#39;], $destName)) {
            return "文件上传失败!";
        }
        return "文件上传成功!";
    } else {
        switch ($fileInfo[&#39;error&#39;]) {
            case 1:
                echo &#39;上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值&#39;;
                break;
            case 2:
                echo &#39;上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值&#39;;
                break;
            case 3:
                echo &#39;文件只有部分被上传&#39;;
                break;
            case 4:
                echo &#39;没有文件被上传&#39;;
                break;
            case 6:
                echo &#39;找不到临时文件夹&#39;;
                break;
            case 7:
                echo &#39;文件写入失败&#39;;
                break;
        }
    }
}
var_dump(upload_file($fileInfo));
Copy after login

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:

Detailed explanation of PHP file upload method and information analysis [with video]

## 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 0The value is 1The value is 2The value is 3The value is 4The value is 6 When the value is 7, no error occurs and the file is uploaded successfully. 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 uploadedThe 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.

After understanding the relevant knowledge of error error numbers, we can design a PHP upload method to make multiple judgments on uploaded files. In the above code, the specific code for method judgment has been written in great detail. First, we defined the upload_file() method, which was given three parameters, which respectively represent file upload information, directory and an array to determine the file type. .

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:

Detailed explanation of PHP file upload method and information analysis [with video]

##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 Tutorial

is [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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template