我在我的消息传递系统中添加了一个上传文件的东西,当我尝试按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 之后的事情,这样它就不会触发两次,而是从不称自己为视频。请帮忙
1 回答
UYOU
TA贡献1878条经验 获得超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 不应该验证ximage/jpeg
- 1 回答
- 0 关注
- 80 浏览
添加回答
举报
0/150
提交
取消