search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the principle of file upload in PHP and the reasons for error reporting

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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!