我正在制作一个用户上传文件的页面。如果文件类型是任何其他 jpg、gif 和 pdf,我想要一个 if 语句创建一个 $error 变量。
这是我的代码:
$file_type = $_FILES['foreign_character_upload']['type']; //returns the mimetype
if(/*$file_type is anything other than jpg, gif, or pdf*/) {
$error_message = 'Only jpg, gif, and pdf files are allowed.';
$error = 'yes';
}
我在构建 if 语句时遇到困难。怎么说呢?
最佳答案
将允许的类型放入数组中并使用 in_array()
.
$file_type = $_FILES['foreign_character_upload']['type']; //returns the mimetype
$allowed = array("image/jpeg", "image/gif", "application/pdf");
if(!in_array($file_type, $allowed)) {
$error_message = 'Only jpg, gif, and pdf files are allowed.';
$error = 'yes';
}
关于php - 我怎样才能只允许在 php 中上传某些文件类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2486329/