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: <form method="post" action="upload.php"> <input type="file" name="img" /> </form> upload.php PHP: <?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.
So... now i found something useful... if a want to get last 3 chars from a string i can use negative values: substr($img_name,0,-3). One more thing. what you do if you have .jpeg extention? there are 4 letters.
you can make an exception for that, generally most jpeg files are named .jpg but you can make a special exception for .jpeg. PHP: else if (substr($img_name,0,-4) == "jpeg", $img_exts) && $img_size < $max_size) { move_uploaded_file($img_temp_loc,"/images" . $img_name); } The above generally should be the first if statement because of the program logical flow, you normally start with the odd ball first because in this case its first going to check if the files extension is in the array thus iterating through the arrays values and then if its not there then check if its jpeg. The full notion would be PHP: <?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 (substr($img_name,0,-4) == "jpeg", $img_exts) && $img_size < $max_size) { move_uploaded_file($img_temp_loc,"/images" . $img_name); } 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."; } } ?> If you do you need to add an else if statement for that with the code to process it or do a conversion based on if its .jpeg to .jpg when your saving the file. Also I dont recommend using negatives in substr() because it may or may not work properly the best case would be to use: PHP: substr($text_var,strlen($text_var) - 3,strlen($text_var)); We use strlen() because it gives the length in numbers per character including white space of the string. In this case subtracting the number of points we wanted to from the end of the file and making it our start number and then using the total length as the end number gives us the same result but with an almost 100% guarantee to work properly over using negatives. version using strlen() PHP: <?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 (substr($img_name,strlen($img_name) - 4,strlen($img_name)) == "jpeg", $img_exts) && $img_size < $max_size) { move_uploaded_file($img_temp_loc,"/images" . $img_name); } else if (in_array(substr($img_name,strlen($img_name) - 3,strlen($img_name)), $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."; } } ?>
the PHP: else if (substr($img_name,0,-4) == "jpeg", $img_exts) && $img_size < $max_size) { move_uploaded_file($img_temp_loc,"/images" . $img_name); } should be PHP: else if (substr($img_name,0,-4) == "jpeg" && $img_size < $max_size) { move_uploaded_file($img_temp_loc,"/images" . $img_name); } and the full versions should be the below, I would have edited them in but I cant edit my posts. none strlen PHP: <?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 (substr($img_name,0,-4) == "jpeg" && $img_size < $max_size) { move_uploaded_file($img_temp_loc,"/images" . $img_name); } 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."; } } ?> strlen version PHP: <?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 (substr($img_name,strlen($img_name) - 4,strlen($img_name)) == "jpeg" && $img_size < $max_size) { move_uploaded_file($img_temp_loc,"/images" . $img_name); } else if (in_array(substr($img_name,strlen($img_name) - 3,strlen($img_name)), $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."; } } ?> I was doing some copy and pasting and forgot to remove the in_array() stuff from the jpeg search sorry about that. Now its done and save memory and processes if the file is a .jpeg instead of searching the arrays values first it does a simpler call to substr(). Ta-Da.