我試圖上傳多個圖像文件。如果圖像名稱是「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) }
}
}
NITPICK:使用標籤元素。 – epascarello
你在'$ _FILES'上做了'var_dump()'嗎? –
錯誤:第一個文件將是'01.jpg',然後是'2.jpg','3.jpg'等等......你需要使用'sprintf('%02d',$ id)'來做你的零-填充。你是否在檢查移動命令的返回值?你是否檢查過$ _FILES中的每個圖像的'['error']'參數以確保它確實上傳了? –