Home>Article>Daily Programming> What are the simple ways to upload multiple files in PHP? (Pictures + Videos)

What are the simple ways to upload multiple files in PHP? (Pictures + Videos)

藏色散人
藏色散人 Original
2018-09-17 17:32:24 10934browse

This article will introduce you to some simple methods for uploading multiple files in PHP.

Before introducing this knowledge content, we have introduced in detail the specific method of uploading a single file in PHP in the previous article [Detailed explanation of PHP file upload method and its information analysis] , then this article is related to the knowledge points to be introduced today. Friends in need can refer to it first.

Below we will introduce you to some simple methods of uploading multiple files in PHP through specific code examples.

The first method: Use a single file upload method

A simple form code is as follows:

    Title 
选择文件进行上传: 选择文件进行上传: 选择文件进行上传:

The code effect is as shown below:

What are the simple ways to upload multiple files in PHP? (Pictures + Videos)

As shown in the figure, we chose to upload three files, and then we uploaded the files to the upload.php file. The PHP code is as follows:

"; var_dump($_FILES);

Then we continue to access the results through the browser as follows:

What are the simple ways to upload multiple files in PHP? (Pictures + Videos)

Here we get the two-dimensional array in the picture, if we want to upload multiple files This needs to be achieved through a foreach loop.

Then the complete code of upload.php is as follows:

"; var_dump($_FILES); $files = []; foreach ($_FILES as $fileInfo) { $files[] = upload_file($fileInfo); } var_dump($files); 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; } } }

Finally we select multiple files to upload, and the result is as shown below:

What are the simple ways to upload multiple files in PHP? (Pictures + Videos)

What are the simple ways to upload multiple files in PHP? (Pictures + Videos)

##At this point, multiple file upload operations have been successfully implemented.


Second method: Using the multiple method in HTML5

Code example of HTML interface for uploading multiple files As follows:

    Title 
选择文件进行上传:

The effect of this code is as follows:

What are the simple ways to upload multiple files in PHP? (Pictures + Videos)

In the form here we use

HTML 5 Multiple attribute, this attribute indicates that the input field can select multiple values, that is, when this attribute is selected, this field can accept multiple values. Multiple is an important attribute for us to implement multiple file uploads. At the same time, we set the name in the input to a file[] array.

Then we select multiple files to upload, as shown below:

What are the simple ways to upload multiple files in PHP? (Pictures + Videos)

What are the simple ways to upload multiple files in PHP? (Pictures + Videos)

As shown in the picture we selected Three pictures, and then use the following upload.php file to receive the three data.

upload.php code example is as follows:

"; var_dump($_FILES); $files = []; foreach ($_FILES as $fileInfo) { $files[] = upload_file($fileInfo); } var_dump($files); 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; } } }

After uploading three files at this time, access through the browser, the result is as follows:

What are the simple ways to upload multiple files in PHP? (Pictures + Videos)

We can see from the picture that the uploaded file information is displayed in the form of a three-dimensional array, in which the names, types, temporary storage location paths, sizes and other information of the three pictures are placed in one in the array.


However, we can find that the information parsing method in the upload.php file has no effect on files uploaded with the multiple attribute.

So this article will first introduce to you these two simple methods of using PHP to implement multiple file uploads using the multiple attribute. In subsequent articles, we will continue to introduce in detail how PHP implements multiple file uploads and information. Analysis is the detailed explanation of the second method!


If you want to know more about PHP, you can follow the PHP Chinese website

PHP Video Tutorial. Welcome everyone to refer to and study!

The above is the detailed content of What are the simple ways to upload multiple files in PHP? (Pictures + Videos). 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