php - 我怎样才能只允许在 php 中上传某些文件类型?

标签 php upload file-type

我正在制作一个用户上传文件的页面。如果文件类型是任何其他 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/

相关文章:

javascript - Angularjs CMS 的 url 中的 GET 请求

c++ - 使用 vim 检测 C++ 标准头文件的文件类型

php - 将文件存储到数据库中并将其上传到网页中

javascript - 如何同步调用异步 JavaScript 函数?

java - php 的非 flash 多文件上传

python - 从 .mat 文件转换为 .txt 文件后文件大小增加

unix - 如何获取二进制文件的魔数(Magic Number)

php - 在 Ubuntu 中使用 Doxygen 和 PHP

php - 如何结合 "Remember me"特性创建安全 session ?

javascript 将冒号附加到变量