我在我的消息傳遞系統(tǒng)中添加了一個上傳文件的東西,當(dāng)我嘗試按file_type對文件進行排序時,它永遠不會起作用。這是我的上傳文件功能:<?phpsession_start(); include_once "../backend/messaging-functions.php"; include_once '../backend/databaseconn.php'; $errors= array(); $file_name = $_FILES['file']['name']; $file_size = $_FILES['file']['size']; $file_tmp = $_FILES['file']['tmp_name']; $file_type = $_FILES['file']['type']; $file_ext= strtolower(end(explode('.',$file_name))); $extensions= array("jpeg","jpg","png","mp4","MP4","MOV","PNG","JPG","mov","JPEG"); if(in_array($file_ext,$extensions)=== false){ $errors[]="extension not allowed, please choose a JPEG, PNG or a MP4 file type!"; } if($file_size > 16777216) { $errors[]='File size must be at MOST 16 MB!'; } if(empty($errors)==true) { move_uploaded_file($file_tmp,'../userfiles/'.$file_name); $fileplace = '../userfiles/'.$file_name; if ($file_type == "image/jpg" or "image/jpeg" or "image/png" or "image/PNG" or "image/JPG" or "image/JPEG") { $date = date("y-m-d h:i:s"); $filetype = "image"; $myId = $_SESSION['user-id']; $message = $fileplace; $groupId = mysqli_real_escape_string($conn, $_GET['id']); addMessage($myId, $groupId, $date, $message, $filetype); } if ($file_type == "video/mp4" or "video/mpeg4") { $date = date("y-m-d h:i:s"); $filedtype = "video"; $myId = $_SESSION['user-id']; $message = $fileplace; $groupId = mysqli_real_escape_string($conn, $_GET['id']); addMessage($myId, $groupId, $date, $message, $filedtype); } } ?>我嘗試添加一個exit();在第一個 if 之后的事情,這樣它就不會觸發(fā)兩次,而是從不稱自己為視頻。請幫忙
1 回答

UYOU
TA貢獻1878條經(jīng)驗 獲得超4個贊
你在中使用的邏輯是有缺陷的——它總是等同于 true。考慮:if( $file_type==....
$file_type='image/jpeg';
if( $file_type == "ximage/jpg" or "ximage/jpeg" or "ximage/png" or "ximage/PNG" or "ximage/JPG" or "ximage/JPEG"){
echo('# this is Bogus #');
}
$filetypes=array( 'image/jpg','image/jpeg','image/png','image/gif');
if( in_array( strtolower( $file_type ), $filetypes ) )echo '# This is good #;';
這將輸出
# this is Bogus ## This is good #;
第一部分顯然是錯誤的 - 使用 etc 不應(yīng)該驗證ximage/jpeg
- 1 回答
- 0 關(guān)注
- 123 瀏覽
添加回答
舉報
0/150
提交
取消