echo '<pre>';
$img = $_FILES['img'];
if(!empty($img))
{
$img_desc = reArrayFiles($img);
print_r($img_desc);
foreach($img_desc as $val)
{
$newname = date('YmdHis',time()).mt_rand().'.jpg';
move_uploaded_file($val['tmp_name'],'./uploads/'.$newname);
}
}
function reArrayFiles($file)
{
$file_ary = array();
$file_count = count($file['name']);
$file_key = array_keys($file);
for($i=0;$i<$file_count;$i++)
{
foreach($file_key as $val)
{
$file_ary[$i][$val] = $file[$val][$i];
}
}
return $file_ary;
}
Array
(
[0] => Array
(
[name] => pix.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php4Ovz7o
[error] => 0
[size] => 6720
)
)
正如我们上面所看到的,它适用于单个文件但是当我上传4个文件时它会继续加载,检查浏览器控制台网络选项卡显示待处理状态
php.ini
如下:
128M > 64M > 32M :: memory_limit > post_max_size > upload_max_filesize
任何人都可以对此有所了解。提前谢谢!
答案 0 :(得分:0)
我已经使用了它并且它有效。我从https://www.developphp.com/video/PHP/Multiple-Files-Simultaneous-Upload-Array-Programming-Tutorial
获得了它所有关于数组以及循环或foreach循环。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="my_parser.php" method="post" enctype="multipart/form-data">
<p><input type="file" name="file_array[]"></p>
<p><input type="file" name="file_array[]"></p>
<p><input type="file" name="file_array[]"></p>
<input type="submit" value="Upload all files">
</form>
</body>
</html>
my_parser.php
<?php
// Script written by Adam Khoury for the following video exercise:
// http://www.youtube.com/watch?v=7fTsf80RJ5w
if(isset($_FILES['file_array'])){
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "upload/".$name_array[$i])){
echo $name_array[$i]." upload is complete<br>";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
}
?>