Home >Backend Development >PHP Tutorial >PHP file upload implementation ideas for multiple file uploads, _PHP tutorial

PHP file upload implementation ideas for multiple file uploads, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:59:56896browse

The implementation idea of ​​multiple file upload in PHP file upload,

Two situations of multiple file upload

①Use multiple name values

<input type="file" name="file1">
<input type="file" name="file2">
<input type="file" name="file3">
<input type="file" name="file4">

a. Data format received after clicking submit

Array
(
[file1] => Array
(
[name] => 8.png
[type] => image/png
[tmp_name] => G:\wamp\tmp\php737.tmp
[error] => 0
[size] => 200
)
[file2] => Array
(
[name] => 28.png
[type] => image/png
[tmp_name] => G:\wamp\tmp\php738.tmp
[error] => 0
[size] => 6244
)
[file3] => Array
(
[name] => 54a296f8n6787b34c.png
[type] => image/png
[tmp_name] => G:\wamp\tmp\php739.tmp
[error] => 0
[size] => 3143
)
[file4] => Array
(
[name] => 54c0573dncb4db6f7.jpg
[type] => image/jpeg
[tmp_name] => G:\wamp\tmp\php788.tmp
[error] => 0
[size] => 5404
)
)

As can be seen from this format, each file corresponds to an array unit

So use foreach to traverse the array and call the file upload function for each array unit

b. Operations after clicking submit

①Receive uploaded file information

$file = $_FILES;

②Introducing the upload function

include('./functions.php');

③Set file saving path

$path = './uploads/'; // 此目录需要手动创建

④Call the file upload function

foreach($file as $v){
$info = uploadFile($v,$path);
⑤判断上传状态
if($info['isok']){
echo '上传成功'.$info['message'];
} else {
echo '上传失败'.$info['message'];
}
}

------------------------------------------------- ----------------

②Use a single name value

a. The first way of writing

<input type='file' name="file[]">
<input type='file' name="file[]">
<input type='file' name="file[]">

b. The second way of writing

<input type="file" name="file[]" multiple>

c. After clicking submit, the data format received

Array
(
[userpic] => Array
(
[name] => Array
(
[0] => 8.png
[1] => 9b2d7581fba543ec9bcf95e91018915a.gif
[2] => 12.jpg
)
[type] => Array
(
[0] => image/png
[1] => image/gif
[2] => image/jpeg
)
[tmp_name] => Array
(
[0] => G:\wamp\tmp\php85E5.tmp
[1] => G:\wamp\tmp\php85E6.tmp
[2] => G:\wamp\tmp\php8635.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 200
[1] => 16503
[2] => 19443
)
)
)

As can be seen from this format, the uploaded file information is saved separately in each subscript.
So what we have to do is to splice out a complete file information, a one-dimensional array

Array(
[name] => 54c0573dncb4db6f7.jpg
[type] => image/jpeg
[tmp_name] => G:\wamp\tmp\php788.tmp
[error] => 0
[size] => 5404
)

So the operation to be performed is to traverse $_FILES['file'] and then retrieve the information of each uploaded file

d. Operations after clicking submit

①Receive uploaded file information

$file = $_FILES['file'];

②Introducing the upload function

include('./functions.php');

③Set file saving path

$path = './uploads/'; // This directory needs to be created manually

④Call the file upload function

foreach($file['name'] as $key=>$value){
$data['name'] = $file['name'][$key];
$data['type'] = $file['type'][$key];
$data['tmp_name'] = $file['tmp_name'][$key];
$data['error'] = $file['error'][$key];
$data['size'] = $file['size'][$key];
$info = uploadFile($data,$path);

⑤ Determine upload status

if($info['isok']){
echo '上传成功'.$info['message'];
} else {
echo '上传失败'.$info['message'];
}
}

a. Traverse $file['name'] just to get $key

b. Each time it is traversed, take out the file information corresponding to the subscript and assign it to the corresponding key in a new array

For example, $key = 0 for the first time;

$data['name'] = $file['name'][0]; // 相当于取出了第一个文件的名字
$data['type'] = $file['type'][0]; // 相当于取出了第一个文件的类型

...

After the first traversal is completed

$data = array(
[name] => 54c0573dncb4db6f7.jpg
[type] => image/jpeg
[tmp_name] => G:\wamp\tmp\php788.tmp
[error] => 0
[size] => 5404
);

This will retrieve all the information of the first file

Then call the upload function to perform file upload processing

$key=1 during the second traversal, which is equivalent to obtaining the information of the second uploaded file

Articles you may be interested in:

  • PHP5 UTF8 multi-file upload class
  • php dynamic multi-file upload
  • The latest php file upload model, support Multi-file upload
  • How to handle multi-file upload of ordinary forms in php
  • Principle and code of php multi-file upload function
  • A simple example of php jquery multi-file upload
  • php multiple file upload implementation code
  • php multiple file upload and download example sharing
  • Thinkphp multiple file upload implementation method
  • PHP multiple file upload class example
  • PHP multiple file upload examples
  • PHP implementation of file upload and multiple file upload

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1096142.htmlTechArticle Implementation ideas of multiple file uploads for PHP file upload, two situations of multi-file upload ① Using multiple name values input type="file" name="file1"input type="file" name="file2"input type="fi...
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