Well some people may or may not know how to do this but I find that its best to explain the process. First were going to upload the file and uses the auto global $_FILE to get the uploaded files info before we save it.

HTML Code:
<form method="post" action="upload.php">
<input type="file" name="img" />
</form>
upload.php

PHP Code:
<?php

$img_exts 
= array("jpg","jpeg","png","gif","tiff","bmp"); // list of allowed file types
$max_size 40000// max file size in bytes

$img_size $_FILE["img"]["size"]; // returns the files size in bytes

$img_type $_FILE["img"]["type"]; // returns the types as image/extenstions_here

$img_name $_FILE["img"]["name"]; //returns the files name including extenstion

$img_temp_loc $_FILE["img"]["tmp_name"]; // returns temp upload location of the image

$img_err $_FILE["img"]["error"]

/* start the process of the validation */

if ($img_err 0)
{
 echo 
"There was an error uploading your image, please check to make sure the file is an image and try again.";
}
else
{

if (
in_array(substr($img_name,0,-3), $img_exts) && $img_size $max_size)
{

move_uploaded_file($img_temp_loc,"/images" $img_name);

}

else
{

echo 
"either your image was to big or the file was not a valid image files please correct this and try again.";

}

}

?>
instead of checking the $img_type variable which returns image/jpeg we check the $img_name since the extension is at the end of the name of the file. We also check to see if the size in bytes is less than our max bytes and if so move it to a permanent location in the webserver. By default the image is stored in the tmp directory of the server and deleted after the script runs. Thats why we moved it so it can be saved in the file system. You can of course rename the file using rename() and if you know GD you can resize the image to.
shabbir like this