By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Determining filetype

P: n/a
Is it possible in PHP to determine whether a file is (intended as)
binary or ascii data other than by examining its extension? I can't find
a ready-made function or script online that does so.

If I open a file with less on my linux box for example it warns when it
'thinks' it's binary rather than ascii.

TIA.
Sh.
Feb 17 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Schraalhans Keukenmeester schreef:
Is it possible in PHP to determine whether a file is (intended as)
binary or ascii data other than by examining its extension? I can't find
a ready-made function or script online that does so.
No, one could make an educated guess however for the types u are expecting.

--
Arjen
http://www.hondenpage.com
Feb 17 '07 #2

P: n/a
Hi,

The "file" command on unix should tell you if the file is text or
binary.

You could also do it with a regexp:

$isBinary = preg_match('/[^\x08\t\n\r\f\x20-\x7e]/',
file_get_contents('/the/file'));

(By the way, less can be made to stop warning you by using the -f
option: less -f /usr/local/bin/php)

On Feb 17, 2:15 pm, Schraalhans Keukenmeester <bitbuc...@invalid.spam>
wrote:
Is it possible in PHP to determine whether a file is (intended as)
binary or ascii data other than by examining its extension? I can't find
a ready-made function or script online that does so.

If I open a file with less on my linux box for example it warns when it
'thinks' it's binary rather than ascii.

TIA.
Sh.

Feb 17 '07 #3

P: n/a
>Is it possible in PHP to determine whether a file is (intended as)
>binary or ascii data other than by examining its extension? I can't find
a ready-made function or script online that does so.

If I open a file with less on my linux box for example it warns when it
'thinks' it's binary rather than ascii.
An 'ascii' file probably does not contain control characters (codes
0 - 31 or 127) other than tab, carriage return, line feed, possibly
form feed, and possibly backspace. Depending on the character set,
it may not contain codes 128 - 255 either.

Feb 18 '07 #4

P: n/a
Schraalhans Keukenmeester wrote:
Is it possible in PHP to determine whether a file is (intended as)
binary or ascii data other than by examining its extension? I can't find
a ready-made function or script online that does so.

If I open a file with less on my linux box for example it warns when it
'thinks' it's binary rather than ascii.
The 'file' command from the shell prompt will give you the file type. There
are a wealth of tools for handling files available from the shell you might
want to read the man pages for 'cat', 'tr' and 'strings' too.

HTH

C.
Feb 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.