0

我正在创建一个允许用户上传图像的脚本,但现在我想包含一个简单的 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
4

3 回答 3

7
    $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";
    }
于 2013-02-09T15:01:19.217 回答
3
$file_type = $_FILES['name']['type']; //returns the mimetype

$allowed = array("image/png", "image/gif", "application/pdf");
if(!in_array($file_type, $allowed)) {
  $error_message = 'Only png, gif files are allowed.';
  $error = 'yes';
}

希望它可以帮助解决问题。

于 2013-02-09T14:55:48.507 回答
0
$path_parts = pathinfo('/path/emptyextension.');
var_dump($path_parts['extension']);

这将返回确切的扩展名,您可以根据需要进行比较。

于 2013-02-09T14:41:39.000 回答