[請益] 判斷使用者上傳檔案疑問

作者: 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