2013-11-04 91 views
-1

我有上傳單個文件和工作正常的情況如下:PHP多文件上傳不處理

<form enctype="multipart/form-data" action="" method="POST"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" /> 
    <input name="userfile" type="file" /> 
    <input type="submit" value="Upload" /> 
</form> 
<?php 

$uploaddir = $campaign['upload_dir'].'/'; 
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']); 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
    echo "File has been successfully uploaded.\n"; 
} else { 
    echo "Upload failed"; 
} 


?> 

當我的Adpat此接受上傳多個文件,它似乎沒有工作。我沒有得到任何錯誤/警告,所以我完全難住。這裏是我的多個文件上傳代碼:

<form enctype="multipart/form-data" action="" method="POST"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" /> 
    <input name="userfile[]" type="file" multiple /> 
    <input type="submit" value="Upload" /> 
</form> 

<?php 

$uploaddir = $campaign['upload_dir'].'/'; 
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$key]); 

foreach ($_FILES["userfile"]["error"] as $key => $error) { 
if ($error == UPLOAD_ERR_OK) { 
$name = $_FILES["userfile"]["name"][$key]; 
move_uploaded_file($_FILES["userfile"]["tmp_name"][$key], $uploadfile); 
{ 
echo "File has been successfully uploaded.\n"; 
} else { 
echo "Upload failed"; 
} 
?> 

有什麼建議可能是錯的?

+1

'$ _FILES [ 「文件」]'是不一樣的'NAME =「userfile的[] 「' – TiMESPLiNTER

+0

將你的'error_reporting'和'display_errors'設置爲明智的開發級別......這樣PHP就可以告訴你這樣的東西! – CBroe

回答

1

您在$_FILES用錯了鍵,你必須使用$_FILES['userfile'],不$_FILES['files']

<form enctype="multipart/form-data" action="" method="POST"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" /> 
    <input name="userfile[]" type="file" multiple /> 
    <input type="submit" value="Upload" /> 
</form> 

<?php 

$uploaddir = $campaign['upload_dir'].'/'; 
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$key]); 

foreach ($_FILES["userfile"]["error"] as $key => $error) { 
if ($error == UPLOAD_ERR_OK) { 
$name = $_FILES["userfile"]["name"][$key]; 
move_uploaded_file($_FILES["userfile"]["tmp_name"][$key], $uploadfile); 
{ 
echo "File has been successfully uploaded.\n"; 
} else { 
echo "Upload failed"; 
} 
?> 
+0

對不起,我忘了更新這個問題。我現在編輯它。 – danyo

0
  <html lang="en"> 
    <head> 
     <meta charset="UTF-8" /> 
     <title>Multiple File Ppload with PHP</title> 
    </head> 
    <body> 
     <form action="" method="post" enctype="multipart/form-data"> 
     <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" /> 
     <input type="submit" value="Upload!" /> 
    </form> 
    </body> 
    </html> 


     <?php 

     $valid_formats = array("jpg", "png", "gif", "zip", "bmp"); 
     $max_file_size = 1024*100; //100 kb 
     $path = "uploads/"; // Upload directory 
     $count = 0; 

     if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ 
      // Loop $_FILES to exeicute all files 
      foreach ($_FILES['files']['name'] as $f => $name) {  
       if ($_FILES['files']['error'][$f] == 4) { 
        continue; // Skip file if any error found 
       }   
       if ($_FILES['files']['error'][$f] == 0) {    
        if ($_FILES['files']['size'][$f] > $max_file_size) { 
         $message[] = "$name is too large!."; 
         continue; // Skip large files 
        } 
        elseif(! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){ 
         $message[] = "$name is not a valid format"; 
         continue; // Skip invalid file formats 
        } 
        else{ // No error found! Move uploaded files 
         if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)) 
         $count++; // Number of successfully uploaded file 
        } 
       } 
      } 
     } 
?>