FIle upload - File types

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
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

FIle upload - File types

Post by guitarlvr »

Hey everyone, long time no see! Hope everyone is doing well. I have a quick question for you all. I have a situation in which i can only accept a certain type of file which is easily attained by $_FILES['type']. This, however, imposes a problem. I am only allowed to accept .tif files. what if someone renames their .exe virus with a .tif extension. I tested it and ['type'] is the type of the file that the extension says it is. Meaning, if they change the exe to tif, it will give .tif properties and not the original .exe. My question is, is there a way to detect the type of the file no matter what extension is on the file? Any help is much appreciated.

Here is the code i am working with:

Code: Select all

<?php
if (isset($_POST['submitted']))
{
// Where the file is going to be placed 
$target_path = "uploads/";

// Add the original filename to our target path. Result is "uploads/filename.extension"
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
$_FILES['uploadedfile']['tmp_name']; 
echo $_FILES['uploadedfile']['type'];
}else{
?>
<HTML>
<body>
<form enctype="multipart/form-data" action="#" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
}
?>
</body>
</html>
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You could try either mime_content_type() or it's successor, Fileinfo
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Also getimagesize() returns type information.
(#10850)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

arborint++

getimagesize() seems to be far better supported than the generic file/mime functions.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

The only problem is that it will also accept any other image file (jpeg, png, etc). If supporting ONLY tif is a requirement, you need fileinfo/mime magic.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

@AC: getimagesize() includes the mime type in it's output! How sweet is that?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Sweet as molasses! Didn't know it could do that. :-)
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

Thank you all very much. getimagesize() returns the correct image type no matter what you change it to. Thanks again guys, hope you all have a very happy holiday!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

3.times {puts 'ho'}
oops - wrong language! :oops:
Post Reply