[请益] 判断使用者上传档案疑问

楼主: mygirl30229 (Cian)   2015-12-01 14:47:26
程式码的第一行if($_FILES)判断使用者是否有上传档案
有设定过 if($_FILES != "null"),if($_FILES != null),if($_FILES !="")
亦设定过$_FILES['newsimg'][name],$_FILES['newsimg'][tmp_name]
以下是我的upload.php
if($_FILES){
$sql = "select no from news where title_tw='$title_tw' or
title_ch='$title_ch' order by no desc;";
$result = mysqli_fetch_array(mysqli_query($con, $sql));
$newsid = $result['no'];
$num = count($_FILES['newsimg']['name']);
for($i = 0 ; $i < $num ; $i++){
$imgname = $_FILES['newsimg']['name'][$i];
$patch = "../images/news/";
$sql = "insert into news_images (newsid,imgname,imgurl) values
('$newsid','$imgname','$patch')";
if(mysqli_query($con, $sql) and
move_uploaded_file($_FILES['newsimg']['tmp_name'][$i],
$patch.$_FILES['newsimg']['name'][$i]))
echo $imgname."上传完毕。<br />";
}
echo "图片均以上传成功。<br />";
}
另外这个表单接收文字讯息与上传档案
程式码中我先储存接收到的文字资料到news,在抓出该笔资料id
接着将id给要储存图片的news_images
感觉步骤不精简,query了好多次
请问是否有更好的办法取代这样的方式呢?
作者: rockmanalpha (KIN)   2015-12-01 17:51:00
要判断上传有没有成功 你要检查$_FILES['newsimg']['error']里面的值请参考 http://tinyurl.com/btb5y
楼主: mygirl30229 (Cian)   2015-12-01 19:56:00
改成if($_FILES[newsimg][name][0] != null){}解决了~~if($_FILES['newsimg']['name'][0] != null){}才对感谢楼上大大,我只是要判别是否有上传档案而已

Links booklink

Contact Us: admin [ a t ] ucptt.com