PHP多图片上传问题,不管我选择几个图片进行上传,但是,只能传上去最后添加的一张

代码如下:
$count=0;
$imgUrlArray = array();

foreach($_FILES["imgfile"]["error"] as $key=>$error){

if($error==UPLOAD_ERR_OK){
$file_name=$_FILES["imgfile"]["name"][$key];

$tmp_name=$_FILES["imgfile"]["tmp_name"][$key];

$temp_arr = explode(".", $file_name);
$file_ext = array_pop($temp_arr);
//array_shift删除前端第一个
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);

$name =$uploaddir.date("YmdHis"). '.'.$file_ext;

$uploadfile=$uploaddir.basename($name);

if(move_uploaded_file($tmp_name,$uploadfile)){$count++;}
}
}
for($i=0;$i<$count;$i++){
$imgUrlArray[] = '/'.$uploaddir.basename($news_file_name);
}
if(!empty($imgUrlArray)){
$postArray['imgurl'] = implode(',',$imgUrlArray);
}

比如我选择4个上传,那么,输出后发现只有最后一个上传上去了,前面三个都没有传上去! 这样应该怎么处理啊?

下面是上传页面的代码
<script type="text/javascript">
function addimg(){
//包含所有文件域的DIV
var div = document.getElementById('imgs');
//文件域
var input = document.createElement("input");
input.name = "imgfile[]";
input.type = 'file';
//添加
div.appendChild(input);
//删除按钮
var button = document.createElement("a");
button.href = "javascript:;";
button.innerHTML = '删除';
div.appendChild(button);
//换行
var br = document.createElement("br");
div.appendChild(br);
//在按钮上增加删除的事件
button.onclick = function(){
input.parentNode.removeChild(input);
this.parentNode.removeChild(this);
br.parentNode.removeChild(br);
}
}
</script>

<form name="myform" action="up.php" method="post" enctype="multipart/form-data">
<input name="id" type="hidden" value="<?php echo $ps["id"];?>" />
上传图片:<div id="imgs">
<input type="file" name="imgfile[]" id="imgfile[]"/><input type="button" onclick="addimg()" value="增加"/><br>
</div>
<br><br>
<?php if($postArray['id']!=''&&$ps['imgurl']!=''){?>
<div class="image">
<?php
if($imgCount!=0){
for($i=0;$i<$imgCount;$i++){
?>
<div class="imgs">
<div class="goDel">
<a href="?act=del_one&id=<?php echo $ps['id'];?>&img=<?php echo $imgUrlArray[$i];?>&imgid=<?php echo $i;?>" onclick="javascript:return confirm('提示:您确定要继续执行该操作吗?')" class="del"></a>
</div>
<a href="<?php echo $imgUrlArray[$i];?>" rel="example_group"><img src="<?php echo $imgUrlArray[$i];?>" /></a>
</div>
<?php } }?>
</div><br><br><br><br>
<?php }?>
<input type="submit" id="button" value="提交">
<input type="reset" value="取消设置" />
</form>
能否在我的代码的基础上修改呢?

第1个回答  2012-07-06
<?php
$db="editor";
$user="admin";
$pas="admin";
$conn=mysql_connect("localhost",$user,$pas) or die("连接错误");
mysql_select_db($db,$conn);
mysql_query("set names 'GB2312'");
$nba=$_FILES["bai"]["name"];
$tmpname=$_FILES["bai"]["tmp_name"];
$size=$_FILES["bai"]["size"];
$type=$_FILES["bai"]["type"];
$error=$_FILES["bai"]["error"];

if (file_exists("../upload/".$nba )){
echo "文件已存在";
}else {
foreach ($error as $key=>$val){
if($val==0){
move_uploaded_file($tmpname[$key],"../upload/" .rand().$nba[$key]);
$imgpath="../upload/" .rand().$nba[$key];
echo $imgpath;
$sql1 = "INSERT INTO news (title, content,imgpath) VALUES ('a','b','$imgpath')";
echo $nba[$key]."--------".$tmpname[$key]."----------".$size[$key]."--------------".$type[$key];
echo "<br>";
}
else {
echo "wrong!";}

}
}
?>

<html>
<body>
<form name="bai" id="bai" method="POST" action="wjsc1.php" enctype="multipart/form-data" >
上传文件<input type="file" name="bai[]" id="bai" />
<br>
<input type="file" name="bai[]" id="bai" />
<br>
<input type="file" name="bai[]" id="bai" />
<br>
<span style="magrin:0px;padding:0px"><input type="image" name="img"/ src="images/submit.gif"></span>
</form>
</body>
</html>

自己对比哇,管家要把数组处理好本回答被提问者和网友采纳
第2个回答  2012-07-09
你的foreach只循环了一遍.
第3个回答  2012-07-06
写的好乱啊,建议去找一个Jquery多图上传的控件来,很轻松几行代码就搞定。