How to Check if File is ASCII or Binary in PHP

后端 未结 5 2053
说谎
说谎 2020-12-03 14:23

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

5条回答
  •  醉话见心
    2020-12-03 14:53

    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

提交回复
热议问题