2016-08-07 67 views
-2
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

我沒有看到任何與如果(isset($ _ FILES [ 'MYFILE'])){}在那裏和調試你應該做的print_r($ _ FILES ); –

+0

你應該把你的表單html放在這裏。 –

回答

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>"; 
     } 
    } 
} 
?>