search
HomeBackend DevelopmentPHP TutorialPHP multiple file upload operation_PHP tutorial

PHP multi-file upload operation

In fact, multiple file uploads and single file uploads are similar. The principles are the same, but there are some tricks in the code.
The first is the index.html upload form, but the file in the previous file upload form is changed to file[]
Copy code
upload files
Upload file:
Upload file:
Upload file:
Copy code
Use $_FILES in upload.php to print it
print_r($_FILES);
?>
Get the following multi-dimensional array
Copy code
Array
(
[file] => Array
(
[name] => Array
          (
                  [0] => Photo 1.jpg
                  [1] => Photo 2.jpg
                                                                                                                                                                                                                                                               
        )
[type] => Array
          (
                                                                                                                                                                   
                                                                                                                                                                                               
                                                                                                                                                                                                           
        )
[tmp_name] => Array
          (
                                                                                                                        [0] => [1] => [2] =>
        )
[error] => Array
          (
                  [0] => 0
                    [1] => 0
                  [2] => 0
        )
[size] => Array
          (
                  [0] => 0
                  [1] => 0
                  [2] => 0
        )
)
)
Copy code
According to the principle of single file upload, first think about what we need to get?
Obviously we need to get an array of file information. The array contains name, type, tmp_name, error, size. What we get at this time is a multi-dimensional array. Although the corresponding key values ​​exist, it is multi-dimensional. ,
We only need to split it, such as the three files above, we only need to split it into the corresponding three file information arrays.
Structure of split array
Copy code
Array
(
[0] => Array
(
             [name] => Photo 1.jpg
[type] => image/jpeg
>
                                                                                                                                                                                                                 ​ >
)
[1] => Array
(
[name] = & gt; Photo 2.jpg
[type] => image/jpeg
>
                                                                                                                                                                                                                 ​ >
)
[2] => Array
(
[name] => Photo 3.jpg
[type] => image/jpeg
>
                                                                                                                                                                                                                 ​ >
)
)
Copy code
The following is the code for splitting and reorganizing the array
Copy code
//print_r($_FILES['file']);
$arr=$_FILES['file'];
$files=array();
for($i=0;$i
$files[$i]['name']=$arr['name'][$i];
$files[$i]['type']=$arr['type'][$i];
$files[$i]['tmp_name']=$arr['tmp_name'][$i];
$files[$i]['error']=$arr['error'][$i];
$files[$i]['size']=$arr['size'][$i];
}
print_r($files);
?>
Copy code
The rest is simple. Just repeat the steps of uploading a single file and iterate through the array.
The code is as follows:
Copy code
//print_r($_FILES['file']);
$arr=$_FILES['file'];
$files=array();
for($i=0;$i $files[$i]['name']=$arr['name'][$i];
$files[$i]['type']=$arr['type'][$i];
$files[$i]['tmp_name']=$arr['tmp_name'][$i];
$files[$i]['error']=$arr['error'][$i];
$files[$i]['size']=$arr['size'][$i];
}
for($i=0;$i //Get uploaded file information
$fileName=$files[$i]['name'];
$fileType=$files[$i]['type'];
$fileError=$files[$i]['type'];
$fileSize=$files[$i]['size'];
$tempName=$files[$i]['tmp_name'];//Temporary file name
//Define upload file type
$typeList = array("image/jpeg","image/jpg","image/png","image/gif"); //Define allowed types
if($fileError>0){
//Judge the error number of uploaded files
switch ($fileError) {
case 1:
$ message = "uploaded files exceeded the value restricted by upload_max_filesize options in php.ini.";
break;
case 2:
                                                                                                                                                                                                $message="The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.";
break;
case 3:
                        $message="Only part of the file has been uploaded.";
break;
case 4:
                            $message="No files uploaded.";
break;
case 6:
$ message = "Can't find the temporary folder.";
break;
case 7:
                        $message="File writing failed";
break;
case 8:
                                $message="File upload interrupted due to PHP extension";
break;
      }
exit("File upload failed: ".$message);
}
if(!is_uploaded_file($tempName)){
//Determine whether it is a file uploaded by POST
exit("It was not uploaded via HTTP POST");
}else{
if(!in_array($fileType, $typeList)){
exit("The uploaded file is not of the specified type");
}else{
if(!getimagesize($tempName)){
                                                                                                                                                            // Prevent users from uploading malicious files, such as changing the virus file extension to image format
exit("The uploaded file is not a picture");
      }
}
if($fileSize>1000000){
                                                                                                                                                                                                              ’ ’       ’ to ’     ’                     out‐‐ together ‐ out‐download of a specific form to be uploaded
                    exit("Uploaded file exceeds limit size");
      }else{
// Avoid the Chinese name garbled in the Chinese name
                                                      $fileName=iconv("UTF-8", "GBK", $fileName);//Convert the character encoding captured by iconv from utf-8 to gbk output
$ filename = strlaplace (".", Time (). ".", $ Filename); // Add the time stamp after the picture name, avoid the re -name file coverage
                                                                                                                                                                                                                                                                                   
                        echo "File uploaded successfully!";
        }else{
                                                                                                                                                                having having  -                                           echo "Failed to upload file";
        }
      }
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/855346.htmlTechArticlePHP multi-file upload operation is actually similar to multi-file upload and single-file upload. The principles are the same, but in the code Did a little trick on it. The first is the index.html upload form, just...
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
wpsystem是什么文件夹wpsystem是什么文件夹Sep 01, 2022 am 11:22 AM

wpsystem文件夹是windows应用文件夹;创建WpSystem文件夹是为了存储某些特定“Microsoft Store”应用程序的数据,因此建议不要删该文件夹,因为删除之后就无法使用指定的应用。

winreagent是什么文件夹winreagent是什么文件夹Aug 26, 2022 am 11:23 AM

winreagent是在系统更新或升级的过程中创建的文件夹;该文件夹中通常包含临时文件,当更新或升级失败时,系统将通过还原先前创建的临时文件来回滚到执行更新或升级过程之前的版本。

baidunetdiskdownload是什么文件夹baidunetdiskdownload是什么文件夹Aug 30, 2022 am 10:45 AM

baidunetdiskdownload是百度网盘默认下载文件的文件夹;百度网盘是百度推出的一项云存储服务,只要下载东西到百度网盘里,都会默认保存到这个文件夹中,并且可跨终端随时随地查看和分享。

usmt.ppkg是什么文件usmt.ppkg是什么文件Sep 09, 2022 pm 02:14 PM

“usmt.ppkg”是windows自带的系统还原功能的系统备份文件;Windows系统还原是在不需要重新安装操作系统,也不会破坏数据文件的前提下使系统回到原有的工作状态,PBR恢复功能的备份文件就是“usmt.ppkg”。

mobileemumaster是什么文件mobileemumaster是什么文件Oct 26, 2022 am 11:28 AM

mobileEmuMaster是手机模拟大师的安装文件夹。手机模拟大师是PC电脑模拟运行安卓系统的免费模拟器程序,一款可以让用户在电脑上运行手机应用的软件,支持安装安卓系统中常见的apk执行文件,支持QQ、微信等生活常用应用,达到全面兼容的效果。

kml是什么文件的格式kml是什么文件的格式Sep 14, 2022 am 10:39 AM

kml是谷歌公司创建的一种地标性文件格式;该文件用于记录某一地点或连续地点的时间、经度、纬度、海拔等地理信息数据,可以被“Google Earth”和“Google Maps”识别并显示。

备份文件的扩展名通常是什么备份文件的扩展名通常是什么Sep 01, 2022 pm 03:55 PM

备份文件的扩展名通常是“.bak”;bak文件是一个备份文件,这类文件一般在'.bak前面加上应该有原来的扩展名,有的则是由原文件的后缀名和bak混合而成,在生成了某种类型的文件后,就会自动生成它的备份文件。

config是什么文件夹可以删除吗config是什么文件夹可以删除吗Sep 13, 2022 pm 03:48 PM

config是软件或者系统中的配置文件,不可以删除;该文件是在用户开机时对计算机进行初始化设置,也就是用户对系统的设置都由它来对计算机进行恢复,因此不能删除软件或者系统中的config配置文件,以免造成错误。

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

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment