2010-01-27 34 views
0

我試圖做一個多文件上傳。
我正在使用「Multiple File Upload Magic With Unobtrusive Javascript與jQuery,php陣列問題多重文件上傳

沒有文件上傳。我很確定這是因爲它將文件放入一個數組中,而我沒有設置PHP來處理數組(我不知道該怎麼做)。任何幫助我做錯了什麼?

在此先感謝! :)

jQuery代碼


$(document).ready(function(){ 
    var fileMax = 12; 
    $('#element_input').after('<div id="files_list"></div>'); 
     $("input.upload").change(function(){ 
      doIt(this, fileMax); 
     }); 
    }); 

    function doIt(obj, fm) { 
     if($('input.upload').size() > fm) {alert('Max files is '+fm); obj.value='';return true;} 
      $(obj).hide(); 
      $(obj).parent().prepend('<input type="file" class="upload" name="fileX[]" />').find("input").change(function() {doIt(this, fm)}); 
     var v = obj.value; 
     if(v != '') { 
      $("div#files_list").append('<div>'+v+'<input type="button" class="remove" value="" /></div>') 
      .find("input").click(function(){ 
      $(this).parent().remove(); 
      $(obj).remove(); 
      return true; 
     }); 
    } 
}; 

HTML代碼


<form action="myPhpCodeIsBelow.php" method="post" enctype="multipart/form-data" name="asdf" id="asdf"> 
    <div id="mUpload"> 
    <input type="file" id="element_input" class="upload" name="fileX[]" /> 
    <input type="submit" value="Upload" /> 
    </div> 
</form> 

PHP代碼


$target = "upload/"; 
$target = $target . $_FILES['fileX']['name']; 
$ok=1; 

if(move_uploaded_file($_FILES['fileX']['tmp_name'], $target)) { 
    echo "The file " . $_FILES['fileX']['name'] . " has been uploaded"; 
    } 
else { 
    echo "There was a problem uploading" . $_FILES['fileX']['name'] . ". Sorry"; 
    } 
+0

你的問題是什麼?什麼不行? – 2010-01-27 22:41:00

+0

如何使腳本上傳文件。現在它不,我沒有得到任何錯誤。 – PHPNooblet 2010-01-27 22:44:39

回答

1

$_FILES數組實際上是這樣的:

array (
    'fileX' => 
    array (
    'name' => 
    array (
     0 => '', 
     1 => 'Temp1.jpg', 
     2 => 'Temp2.jpg', 
    ), 
    'type' => 
    array (
     0 => '', 
     1 => 'image/jpeg', 
     2 => 'image/jpeg', 
    ), 
    'tmp_name' => 
    array (
     0 => '', 
     1 => '/tmp/php52.tmp', 
     2 => '/tmp/php53.tmp', 
    ), 
    'error' => 
    array (
     0 => 4, 
     1 => 0, 
     2 => 0, 
    ), 
    'size' => 
    array (
     0 => 0, 
     1 => 83794, 
     2 => 105542, 
    ), 
), 
) 

這意味着你的代碼應該看起來更像是這樣的:

foreach($_FILES['fileX']['name'] as $index => $name) { 
    if(empty($name)) continue; 

    $target = "upload/"; 
    $target = $target . $name; 
    $ok=1; 

    if(move_uploaded_file($_FILES['fileX']['tmp_name'][$index], $target)) 
    { 
     echo "The file " . $name . " has been uploaded"; 
    } 
    else 
    { 
     echo "There was a problem uploading" . $name . ". Sorry"; 
    } 
} 

而且你應該學會縮進代碼更好!

+0

我現在就去試試。謝謝 – PHPNooblet 2010-01-27 23:34:12

+1

它的工作!再次感謝。 – PHPNooblet 2010-01-28 00:15:14