我正在创建一个脚本,该脚本允许用户上传图像,但是现在我想包含一个简单的if语句,该语句仅接受gif和png。这是我的代码,但似乎无法正常工作。
#extention filter
$allowed = array("image/gif","image/png");
if(!in_array($files,$allowed)){
$error_message = 'Only jpg, gif, and pdf files are allowed.';
$error = 'yes';
echo $error_message;
exit();
} #extention
Run Code Online (Sandbox Code Playgroud)
$filename=$_FILE['name']['filename_in_html']; //you can change this with your filename
$allowed='png,jpg'; //which file types are allowed seperated by comma
$extension_allowed= explode(',', $allowed);
$file_extension= pathinfo($filename, PATHINFO_EXTENSION);
if(array_search($file_extension, $extension_allowed))
{
echo "$extension_allowed allowed for uploading file";
}
else
{
echo "$extension_allowed is not allowed for uploading file";
}
Run Code Online (Sandbox Code Playgroud)