Page 1 of 1

FIle upload - File types

Posted: Wed Dec 19, 2007 4:59 pm
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>

Posted: Wed Dec 19, 2007 6:35 pm
by Kieran Huggins
You could try either mime_content_type() or it's successor, Fileinfo

Posted: Wed Dec 19, 2007 6:41 pm
by Christopher
Also getimagesize() returns type information.

Posted: Wed Dec 19, 2007 6:47 pm
by Kieran Huggins
arborint++

getimagesize() seems to be far better supported than the generic file/mime functions.

Posted: Wed Dec 19, 2007 6:53 pm
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.

Posted: Wed Dec 19, 2007 10:18 pm
by Kieran Huggins
@AC: getimagesize() includes the mime type in it's output! How sweet is that?

Posted: Wed Dec 19, 2007 10:21 pm
by Ambush Commander
Sweet as molasses! Didn't know it could do that. :-)

Posted: Thu Dec 20, 2007 9:22 am
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!

Posted: Thu Dec 20, 2007 9:46 am
by Kieran Huggins

Code: Select all

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