There are modules that will determine file type for you, however if you just want to check for a text file you can probably just use this heuristic:
my $file = 'C:/tmp.zip'; open F, $file or die $!; read( F, my $buf, 1024 ); close F; print is_text($buf) ? "$file is TEXT\n" : "$file is BIN\n"; # alternatively you can just... print -T $file ? "-T $file is TEXT\n" : "-T $file is BIN\n"; sub is_text { my ($data) = @_; return 0 unless $data; return 0 if $data =~ m![\x00-\x08\x0b\x0e-\x1f]!; return 1; }
All it does is look for any chars that should not be in a TEXT file but are found in binary files. The 1024 char is arbitrary but is the one used by file. There is also the -T file test in Perl which does something similar but needs a real file or handle rather than just a data buffer...
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
In reply to Re: CGI File Upload Security
by tachyon
in thread CGI File Upload Security
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |