如何在PHP中检查文件是ASCII还是二进制 [英] How to Check if File is ASCII or Binary in PHP

查看:65
本文介绍了如何在PHP中检查文件是ASCII还是二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种快速,简单的方法来检查文件是ASCII还是PHP二进制文件?

Is there a quick, simple way to check if a file is ASCII or binary with PHP?

推荐答案

这仅适用于PHP> = 5.3.0,并且并非100%可靠,但是,嘿,这真是太接近了.

This only works for PHP>=5.3.0, and isn't 100% reliable, but hey, it's pretty darn close.

// return mime type ala mimetype extension
$finfo = finfo_open(FILEINFO_MIME);

//check to see if the mime-type starts with 'text'
return substr(finfo_file($finfo, $filename), 0, 4) == 'text';

http://us.php.net/manual/en/ref. fileinfo.php

这篇关于如何在PHP中检查文件是ASCII还是二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆