TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-04-2008, 04:49 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default Image Uploading

When making an upload script, does the name key ( e.g.
PHP Code:
$_FILES['image']['name'
) contain the file type aswell? (e.g. .JPG) or do I have to just get that from
PHP Code:
$_FILES['image']['type'
Just wondering. :]
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 02-04-2008, 05:00 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Edit: I feel like an idiot. Yes it does contain the extention. But the type var does not contain data like .jpg

PHP: Handling file uploads - Manual
A complete reference on that.

For your question, type returns the MIME type. But do NOT rely on this because it is not checked for authenticity and can be forged. What I do is this

PHP Code:
$filetype2 explode('.',$file_name);
$filetype $filetype2[sizeof($filetype2)-1];
$filetype strtolower($filetype);


if(
$filetype == 'jpeg' ||
$filetype == 'jpg')
{
//continue

That gets the value after the last period (so filename.jpg.php cant trick it like it can some). As long as the last extension is acceptable, the server will run it as that no matter what content.

Note: We subtract one from sizeof because it starts at one, not zero like arrays do.
__________________

Village Idiot is offline  
Reply With Quote
Old 02-04-2008, 06:43 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

You state that people shouldn't rely on the MIME type passed along with the uploaded file, because it is not checked for authenticity and can be forged, but have no qualms with 'trusting' the file extension?

Also, using an alternative method, the extension can be grabbed with: $ext = pathinfo($filename, PATHINFO_EXTENSION);

Finally, to answer the original question posed by Orc, $_FILES[...]['name'] will contain the name of the file as sent by the browser (eg. myimage.jpg), extension and all.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Orc (02-06-2008)
Old 02-05-2008, 12:49 AM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

If the files name extension is correct it cannot pose a threat, the server runs files based off what those letters are. It doesn't matter what code you put in a jpg, it cant execute a script unless there is some serious error with the server OS.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Orc (02-06-2008)
Old 02-06-2008, 06:52 PM   #5 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Thanks guys. I was just trying to only strict .GIF .PNG .JPG image types, to others by throwing an exception if they don't match with gif nor png nor jpg.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 02-07-2008, 12:22 PM   #6 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

Im in the process of developing a fileserver platform for personal use and for the fun of it, powered by PHP5 and MySQL.

And so far i´ve built in Mime checks, and Size checks. and will use Salathe´s method to check the file extensions also.

For double security.

Would one check for something else ?

I was thinking about using some image functions to make sure "image.jpg" are in fact a image file.

/EyeDentify
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 02-07-2008, 03:08 PM   #7 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Either one should work, I dont see why you need to be redundant. If you want to make tripple sure it is a valid image, run it though a GD process, it will return an error if the image isnt valid. Although for speed reasons that would be a ridiculous measure.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Orc (02-07-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:53 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design