2013-01-18 25 views
0

我試圖上傳多個圖像文件。如果圖像名稱是「icon.jpg」,那麼它將被移動到與其他圖像不同的位置 - 這似乎工作正常,但只有第一個圖像被上傳和移動。

即使我試圖上傳至少4或5個其他圖像,腳本在第一張圖像之後停止並顯示「查詢爲空」。

$id = '01'; 
foreach($_FILES['file']['name'] as $n => $image) 
if(!empty($image)) { 
{ 
if ($image == 'icon.jpg'){ 
    $target_path = "../images/articles/".$id."_small.jpg"; 
    } else { 
    $target_path = "../images/galleries/".$id."/pk_hb" . $id.".jpg"; 
    } 
    move_uploaded_file($_FILES['file']['tmp_name'][$n], $target_path); 
$id++; 
} 
} 

我包括我使用的是動態JS上傳表單,如果是這樣的問題:

<form action="<? echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data" method="post" name="add"> 
Image 1: <input name='file[]' type='file' accept="image/*" /> 
<div id='file_tools'> 
    <img src='../images/top/file_add.png' id='add_file' title='Add new input'/> 
    <img src='../images/top/file_del.png' id='del_file' title='Delete'/> 
</div> 

<script type='text/javascript'> 
$(document).ready(function(){ 
var counter = 2; 
$('#del_file').hide(); 
$('img#add_file').click(function(){ 
    $('#file_tools').before('Image '+counter+': <input name="file[]" type="file" accept="image/*" />'); 
    $('#del_file').fadeIn(0); 
counter++; 
}); 
$('img#del_file').click(function(){ 
    if(counter==3){ 
     $('#del_file').hide(); 
    } 
    counter--; 
    $('#f'+counter).remove(); 
}); 
}); 
</script> 
    <input class="submit" type="submit" name="add" value="Submit" /> 
</form> 

的var_dump添加的內容($ _ FILES)

array(1) { 
["file"]=> array(5) 
{ 
["name"]=> array(1) { [0]=> string(11) "cooley1.jpg" } 
["type"]=> array(1) { [0]=> string(10) "image/jpeg" } 
["tmp_name"]=> array(1) { [0]=> string(18) "/var/tmp/phpLYaj7U" } 
["error"]=> array(1) { [0]=> int(0) } 
["size"]=> array(1) { [0]=> int(106241) } 
} 
} 
+0

NITPICK:使用標籤元素。 – epascarello

+0

你在'$ _FILES'上做了'var_dump()'嗎? –

+0

錯誤:第一個文件將是'01.jpg',然後是'2.jpg','3.jpg'等等......你需要使用'sprintf('%02d',$ id)'來做你的零-填充。你是否在檢查移動命令的返回值?你是否檢查過$ _FILES中的每個圖像的'['error']'參數以確保它確實上傳了? –

回答

1

更新

$_POST不同,你cann通過將所有inputname設置爲file[],將文件陣列發送到$_FILES$_FILES自動包含您上傳到數組中的所有文件:)其中密鑰爲文件input元素的name

你必須更改名稱的每個文件inputfile_1file_2,... file_n使腳本明白,你有多個文件被上傳。

您還必須檢查您的max-file-uploadsphp.ini的有效值爲http://php.net/manual/en/ini.core.php#ini.max-file-uploads

+0

好吧,我想我明白了。我已經編輯了JS,以便每個輸入都是'file_1','file_2' ...等等,但是我應該如何將'file'屬性更改爲'foreach($ _ FILES ['file'] ['name']] 'file_1','file_2'等? – John0990

+0

您更改爲'foreach($ _ FILES as $ fieldName => $ obj)''fieldName''將包含'input'元素的名稱('file_1', 'file_2',...),而'obj'將會是一個包含'name','type','tmp_name'的數組...... –

+0

我已經改變了'foreach',正如你所說的,但我仍然得到了'Query var_dump:'array(1){[「file_1」] => array(5){[「name」] => string(8)「icon.jpg」[「type」] = > string(10)「image/jpeg」[「tmp_name」] => string(18)「/ var/tmp/phpLYaj7U」[「error」] => int(0)[「size」] => int(12296 )}}' ' – John0990

相關問題