php - 在输入类型文件浏览窗口中只显示图像文件?

标签 php jquery html

<分区>

所以基本上如何过滤窗口弹出窗口以仅在我单击输入类型文件时显示图像文件

 <input type="file" />

感谢任何帮助,我尝试为此找到解决方案

谢谢

最佳答案

您可以使用它,但它不适用于所有浏览器:

<input type="file" accept="image/*">

在 Chrome 中,您甚至可以指定允许的格式:

<input type="file" accept="image/x-png, image/gif, image/jpeg" />

LIVING DEMO

如果要限制图片类型you can still use JavaScript to validate it在客户端。

当然你也应该用 PHP 在服务器端检查它:

//defining the allowed extensions and types for our system.
$allowedMimes = array('image/gif', 'image/jpeg', 'image/jpg', 'image/png', 'image/bmp', 'image/wbmp');
$allowedExtensions = array('jpg', 'gif', 'jpeg', 'png', 'bmp', 'wbmp');

$extension = end(explode('.',$imgFile));

//is the extension allowed?
if(in_array($extension, $allowedExtensions)){

    //getting the mime type (it can be different from the extension) Be careful!
    $imgInfo = getimagesize('yourPath/'.$imgFile);
    $type = strtolower($imgInfo['mime']);

    //checking the mime type
    if(!in_array($type, $allowedMimes)){
         // is an allowed image type
    }
}

关于php - 在输入类型文件浏览窗口中只显示图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16832255/

相关文章:

php - 从 php、javascript 中的 url(网页)解析特定内容

php - PHP 中的长字符串加密和解密

php - Wordpress + Paypal 在 Paypal Payment 上更改用户角色

javascript - 在 Semantic UI React 中隐藏损坏的图像链接

php - foreach 访问索引或关联数组

javascript - 事件多次发生

javascript - 单击时选择 TD 的值以简化复制

javascript - 使用标题和标题文本折叠面板

asp.net - 有没有办法以编程方式将项目添加到 <UL> 元素?

html - 内嵌高度修复的标签和复选框