如何只允许某些文件类型在php中上传?

我正在创建一个页面,供用户上传文件。如果文件类型是其他jpg,gif和pdf,我希望使用if语句创建$ error变量。

这是我的代码:

$file_type = $_FILES['foreign_character_upload']['type']; //returns the mimetypeif(/*$file_type is anything other than jpg, gif, or pdf*/) {
  $error_message = 'Only jpg, gif, and pdf files are allowed.';
  $error = 'yes';}

我在构造if语句时遇到困难。我怎么说呢


SMILET
浏览 950回答 3
3回答

繁华开满天机

将允许的类型放入数组并使用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';}

桃花长相依

另请参见Zend Framework的Zend_File_Transfer_Adapter_Http和 Zend_Form_Element_File。您可以添加多个不同的验证器,例如最小图像分辨率,MIME类型,最小文件大小,允许的文件扩展名等。
打开App,查看更多内容
随时随地看视频慕课网APP