Page 1 of 1
If it's not an image what is it?
Posted: Thu Aug 05, 2004 3:10 pm
by hawleyjr
I can determine if a file is an image by using the [php_man]getimagesize[/php_man]. How do I determine what type of file it is if it isn't an image?
Code: Select all
<?php
//ARRAY OF FILE NAMES ON MY SERVER:
$a_doc = array('test.doc','htmfile.htm','myimage.jpg');
$doc_root = '/myfilefolder/';
foreach($a_doc as $ak => $docName){
//IS FILE IMAGE:
if(is_array(getimagesize($doc_root.$docName))){
echo $docName.' is an image<BR>';
}else{
//IF THE FILE ISN'T AN IMAGE WHAT IS IT?????
}
}
?>
Posted: Thu Aug 05, 2004 3:44 pm
by feyd
.doc files are binary, .html files are text..
I've had several posts previously involving detection of datatype (via bytestream content) ...
viewtopic.php?t=23517&highlight=flash+text
Posted: Thu Aug 05, 2004 3:50 pm
by hawleyjr
feyd wrote:.doc files are binary, .html files are text..
If a file is binary it could be almost anything? I guess I'll have to create an index of file extensions associated with file types.
Posted: Thu Aug 05, 2004 4:02 pm
by feyd
yeah.. binary files can be literally anything.. but, the bulk of binary files are a specific format structure.. you'll just need to find the formatting template for their binary pattern to determine their type.. this is a lot of work.. so it can be very painful..
Posted: Thu Aug 05, 2004 4:02 pm
by pickle
When you upload a file, you could check the mime type.
Posted: Thu Aug 05, 2004 4:03 pm
by hawleyjr
Yeah, I thought about that however, mime types are easy to fake.
Posted: Thu Aug 05, 2004 4:07 pm
by pickle
Why do you need to know? If someone is uploading a file and it's not what you ask of them, is it possible to just trash it?
Posted: Thu Aug 05, 2004 4:10 pm
by hawleyjr
Yeah it is, and honestly I'm thinking more then I need to. In reality if a user does upload a "bad" file I will know who the user is. I just wanted to display an image next to the file type coinciding with the file type.
Posted: Thu Aug 05, 2004 4:13 pm
by feyd
well. apache just displays a generic "binary" symbol next to anything it doesn't recognize so you could do that..
Posted: Thu Aug 05, 2004 4:16 pm
by hawleyjr
I'm thinking the best way to do this is capture the mime file type when the user uploads the file and store it in a table. I'll just call it from there.