How to Check if File is ASCII or Binary in PHP

后端 未结 5 2041
说谎
说谎 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:56

    Since ASCII is just an encoding for text, with binary representation, not really. You could check that all bytes are less than 128, but even this wouldn't guarantee that it was intended to be decoded as ASCII. For all you know it's some crazy image format, or an entirely different text encoding that also has no use of all eight bits. It might suffice for your use, though. If you just want to check if a file is valid ASCII, even if it's not a "text file", it will definitely suffice.

提交回复
热议问题