graham23s Posted March 30, 2008 Share Posted March 30, 2008 Hi Guys, i can't understand this basic code i have written here: <?php // some vars for the image // $image_file_name = $_FILES['image']['name']; $image_file_size = $_FILES['image']['size']; $image_file_temp = $_FILES['image']['tmp_name']; $image_file_type = $_FILES['image']['type']; // do an extension check before it goes to the function for resizing // // first check what the files extension is and lower case it // // this takes away 4 characters from the end of the file name // $file_extention = substr("$image_file_name", - 4); $file_extention = strtolower($file_extention); // if the file doesn't have the correct extension then reject // if($file_extention != ".jpg" || $file_extention != ".gif" || $file_extention != ".png") { standard_error("Error","Your image file is not one of the allowed types only <b>.jpg</b> <b>.gif</b> and <b>.png</b> are allowed to be uploaded."); } ?> when i test upload an image with .jpg,.gif.png it hits the error, even though the extension is valid, it looks straight forward enough but keep giving me the error, is it asyntax issue at all? thanks guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/98657-basic-upload-code-error/ Share on other sites More sharing options...
wildteen88 Posted March 30, 2008 Share Posted March 30, 2008 Rather than using the following to get the file extension: $file_extention = substr("$image_file_name", - 4); $file_extention = strtolower($file_extention); Use the pathinfo function: $file_extention = pathinfo($image_file_name, PATHINFO_EXTENSION); Quote Link to comment https://forums.phpfreaks.com/topic/98657-basic-upload-code-error/#findComment-504907 Share on other sites More sharing options...
lewis987 Posted March 30, 2008 Share Posted March 30, 2008 or you can explode the file name at the "." so your code would look like: <?php // some vars for the image // $image_file_name = $_FILES['image']['name']; $image_file_size = $_FILES['image']['size']; $image_file_temp = $_FILES['image']['tmp_name']; $image_file_type = $_FILES['image']['type']; // do an extension check before it goes to the function for resizing // // first check what the files extension is and lower case it // // This explodes the string at "."'s, then countsd how many "."s there are // // Then gives the file extension $explode = explode(".", $image_file_name); $count = count($explode) -1; // Remember and minus 1 from the count, as the var count will show one more than there should be. $file_extention = $explode[$count]; // if the file doesn't have the correct extension then reject // if($file_extention != "jpg" || $file_extention != "gif" || $file_extention != "png") { standard_error("Error","Your image file is not one of the allowed types only <b>.jpg</b> <b>.gif</b> and <b>.png</b> are allowed to be uploaded."); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98657-basic-upload-code-error/#findComment-505044 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.