Home  >  Article  >  Backend Development  >  Detailed explanation of the principle of file upload in PHP and the reasons for error reporting

Detailed explanation of the principle of file upload in PHP and the reasons for error reporting

伊谢尔伦
伊谢尔伦Original
2017-06-27 14:08:132145browse

Upload principle and configuration

1.1 Principle

Upload the client file to the server, and then move the server-side file (temporary file) to the specified directory. Can.

1.2 Client configuration

Required: form page (select upload file);

Specifically: the sending method is POST, add enctype="multipart/form- data"Attribute, both are indispensable (however, advantages and disadvantages coexist, here also limits the upload method and subsequent calling of the uploaded file, which will be discussed later)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doAction.php" method="post" enctype="multipart/form-data">
请选择您要上传的文件:
<input type="file" name="myFile" /><br/>
<input type="submit" value="上传"/>
</form>
<?php
?>
</body>
</html>

First is the form page (please ignore the front-end issues automatically...), the key is the attribute of the form; the other is the use of type="file" in the input (reflecting the powerful expansion of PHP, etc.).

Then doAction

<?php
//$_FILES:文件上传变量
//print_r($_FILES);
$filename=$_FILES[&#39;myFile&#39;][&#39;name&#39;];
$type=$_FILES[&#39;myFile&#39;][&#39;type&#39;];
$tmp_name=$_FILES[&#39;myFile&#39;][&#39;tmp_name&#39;];
$size=$_FILES[&#39;myFile&#39;][&#39;size&#39;];
$error=$_FILES[&#39;myFile&#39;][&#39;error&#39;];
//将服务器上的临时文件移动到指定位置
//方法一move_upload_file($tmp_name,$destination)
//move_uploaded_file($tmp_name, "uploads/".$filename);//文件夹应提前建立好,不然报错
//方法二copy($src,$des)
//以上两个函数都是成功返回真,否则返回false
//copy($tmp_name, "copies/".$filename);
//注意,不能两个方法都对临时文件进行操作,临时文件似乎操作完就没了,我们试试反过来
copy($tmp_name, "copies/".$filename);
move_uploaded_file($tmp_name, "uploads/".$filename);
//能够实现,说明move那个函数基本上相当于剪切;copy就是copy,临时文件还在
//另外,错误信息也是不一样的,遇到错误可以查看或者直接报告给用户
if ($error==0) {
    echo "上传成功!";
}else{
    switch ($error){
        case 1:
            echo "超过了上传文件的最大值,请上传2M以下文件";
            break;
        case 2:
            echo "上传文件过多,请一次上传20个及以下文件!";
            break;
        case 3:
            echo "文件并未完全上传,请再次尝试!";
            break;
        case 4:
            echo "未选择上传文件!";
            break;
        case 5:
            echo "上传文件为0";
            break;
    }
}

First look at the information of print_r($_FILES)

Array
(
    [myFile] => Array
        (
            [name] => 简历.doc
            [type] => application/msword
            [tmp_name] => D:\wamp\tmp\php1D78.tmp
            [error] => 0
            [size] => 75776
        )
)

So what you get is a two-dimensional array, what should I do? Use, they are all basic things (in fact, I like to reduce the dimension and then use it);

is basically something that can be understood at a glance, not wordy, there are two key points: tmp_name temporary file name; error error message (code name , can be used later);

Then let’s take a look at the latter part of doAction, using error information to feed back to the user. What needs to be explained is why the error is reported, and what the error message is;

1.3 About Error

--Error reason

Basically exceeds or does not comply with the server's configuration for uploading files. So what are the server-side configurations?

First consider uploading what we used? POST, upload

So look for these items in php.ini:

file_upload:On

upload_tmp_dir=——Temporary file saving directory;

upload_max_filesize=2M

max_file_uploads=20——The maximum number of files allowed to be uploaded at one time (note the difference from the above one, don’t think about it if there is size or not)

post_max_size=8M——The maximum value of data sent in post mode

Other related configurations

max_exectuion_time=-1——The maximum execution time to avoid the program from occupying server resources;

max_input_time=60

max_input_nesting_level=64——Input nesting depth;

memory_limit=128M——Maximum independent memory usage of a single thread

In short, they are all related resources Configuration.

--Error number

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 limit of 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 is uploaded.
UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded.

Note: This error message is the information uploaded in the first step, that is, uploaded to the temporary folder, not the case of move or copy.

The above is the detailed content of Detailed explanation of the principle of file upload in PHP and the reasons for error reporting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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