If it's not an image what is it?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

If it's not an image what is it?

Post 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?????
}

}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

When you upload a file, you could check the mime type.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Yeah, I thought about that however, mime types are easy to fake.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well. apache just displays a generic "binary" symbol next to anything it doesn't recognize so you could do that..
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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.
Post Reply