How to add file type checking in this code

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
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

How to add file type checking in this code

Post by Calimero »

Code: Select all

<?php

if(!empty($_FILES) && $_FILES['img1']['size'] < 200000 /* max size */ && $_FILES['img1']['size'] > 0) 
  move_uploaded_file($_FILES['img1']['tmp_name'], '/path/to/new/location/'.$_FILES['img1']['name']); 

?>
1) I need PHP to check if the file has the extension of bmp, gif, png, or jpg(jpeg), and if it returns true to continue with uploading.

2) image size is now number 200000, but in what units - is it 200kb or 200mb, I'm not sure so please, little help.

Thanks ahead !
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use [php_man]getimagesize[/php_man]():

Code: Select all

if($imgdata = @getimagesize($_FILES['img1']['tmp_name']))
print_r($imgdata); // all the data inside the array
else
die('not a known image format');
the size is ALWAYS in bytes. so 200,000 is 200KB, roughly..
Post Reply