divinedesigns1 Posted October 8, 2015 Share Posted October 8, 2015 ok so i have been getting this problem for the pass year or so, please be advice it was two issues before and i have resolve 1/2 issues. my last issues is base on making sure that the format of the image or file matches with those i wish to be uploaded. // this is the formats$allowed = array('png', 'jpg', 'jpeg', 'gif');// if the format doesnt match display thisif($ptype == $allowed){ $errors[] = 'This format isnt allow</br>Accepted formats are as followed: PNG, JPG, JPEG, GIF';} i also tried this just to make sure i wasnt doing anything wrong if($ptype != $allowed){ $errors[] = 'This format isnt allow</br>Accepted formats are as followed: PNG, JPG, JPEG, GIF'; }else($ptype == $allowed){ $errors[] = 'ok'; } i still ended up with the same issues, which by pass the statement and allowed the format anyway. AS ALWAYS any tips, hint, or advice will do 1 Link to comment Share on other sites More sharing options...
dsonesuk Posted October 8, 2015 Share Posted October 8, 2015 (edited) You are comparing $ptype (which should be one of the four IN array) with a ARRAY? NOT the items IN the array, in_array() is what you are looking for http://www.w3schools.com/php/func_array_in_array.asp. Edit: hope you have allowed for uppercase JPG etc, and also jpg comes in jpeg, jpe as well. Edited October 8, 2015 by dsonesuk Link to comment Share on other sites More sharing options...
divinedesigns1 Posted October 8, 2015 Author Share Posted October 8, 2015 You are comparing $ptype (which should be one of the four IN array) with a ARRAY? NOT the items IN the array, in_array() is what you are looking for http://www.w3schools.com/php/func_array_in_array.asp. Edit: hope you have allowed for uppercase JPG etc, and also jpg comes in jpeg, jpe as well. thanks, didnt know about the jpe Link to comment Share on other sites More sharing options...
dsonesuk Posted October 13, 2015 Share Posted October 13, 2015 (edited) ???????? Why have repeated in another topic? Edited October 13, 2015 by dsonesuk Link to comment Share on other sites More sharing options...
divinedesigns1 Posted October 13, 2015 Author Share Posted October 13, 2015 ???????? Why have repeated in another topic? i forgot i made this topic Link to comment Share on other sites More sharing options...
divinedesigns1 Posted October 13, 2015 Author Share Posted October 13, 2015 ???????? Why have repeated in another topic? ok, so im using the in_array() dont i have to use it against the variable im checking for? Link to comment Share on other sites More sharing options...
dsonesuk Posted October 13, 2015 Share Posted October 13, 2015 (edited) You are looking for something like this $ptype = "JPG"; $errors = array(); //$ptype = "bmp"; //test by uncommenting// this is the formats $allowed = array('png', 'jpg', 'jpeg', 'jpe', 'gif');// if the format doesnt match display this if (!in_array(strtolower($ptype), $allowed)) { $errors[] = '<br>This format isnt allow</br>Accepted formats are as followed: PNG, JPG, JPEG, JPE, GIF'; } foreach ($errors as $error) { echo $error; } Edited October 13, 2015 by dsonesuk Link to comment Share on other sites More sharing options...
divinedesigns1 Posted October 13, 2015 Author Share Posted October 13, 2015 You are looking for something like this $ptype = "JPG"; $errors = array(); //$ptype = "bmp"; //test by uncommenting// this is the formats $allowed = array('png', 'jpg', 'jpeg', 'jpe', 'gif');// if the format doesnt match display this if (!in_array(strtolower($ptype), $allowed)) { $errors[] = '<br>This format isnt allow</br>Accepted formats are as followed: PNG, JPG, JPEG, JPE, GIF'; } foreach ($errors as $error) { echo $error; } so that should work even with this $ptype = $_FILES['imgs']['type']; Link to comment Share on other sites More sharing options...
dsonesuk Posted October 13, 2015 Share Posted October 13, 2015 If $ptype matches what is in the array yes, if not you have make what is in array matches what $_FILES['imgs']['type']; returns. Link to comment Share on other sites More sharing options...
divinedesigns1 Posted October 13, 2015 Author Share Posted October 13, 2015 If $ptype matches what is in the array yes, if not you have make what is in array matches what $_FILES['imgs']['type']; returns. thats the thing, i tried that and it still doesnt give out the correct respond Link to comment Share on other sites More sharing options...
justsomeguy Posted October 13, 2015 Share Posted October 13, 2015 So, what are you doing to figure out the problem? Are you verifying anything at all or just trying to guess why it doesn't work? Link to comment Share on other sites More sharing options...
divinedesigns1 Posted October 13, 2015 Author Share Posted October 13, 2015 So, what are you doing to figure out the problem? Are you verifying anything at all or just trying to guess why it doesn't work? actually i went around a few ways to figure out $ptype is holding any value, also i echo out $ptype, i used the $allowed array to select each value one by one $allowed[0], etc. i have even tried the foreach() different if statements and now im about to do var_dump and print_r Link to comment Share on other sites More sharing options...
justsomeguy Posted October 13, 2015 Share Posted October 13, 2015 And if you print the value you're checking, and the values in the array, what do you find? Link to comment Share on other sites More sharing options...
divinedesigns1 Posted October 14, 2015 Author Share Posted October 14, 2015 (edited) And if you print the value you're checking, and the values in the array, what do you find? getting a boolean of false once i upload a jpg, etc but boolean of true once i upload a txt or doc file when i var_dump() the $ptype i get this once i try to upload a doc or txt file string 'application/octet-stream' (length=24) and i get this once i try to upload the correct file string 'image/gif' (length=9) so im not sure how the file isnt being stopped once i try to compare it to the array Edited October 14, 2015 by DDs1 Link to comment Share on other sites More sharing options...
dsonesuk Posted October 14, 2015 Share Posted October 14, 2015 you are checking mime type against extension, 'image/gif' is mime type you need to use pathinfo($target_file,PATHINFO_EXTENSION); to retrieve extension as used here http://www.w3schools.com/php/php_file_upload.asp Link to comment Share on other sites More sharing options...
divinedesigns1 Posted October 14, 2015 Author Share Posted October 14, 2015 you are checking mime type against extension, 'image/gif' is mime type you need to use pathinfo($target_file,PATHINFO_EXTENSION); to retrieve extension as used here http://www.w3schools.com/php/php_file_upload.asp wooot wooot it working thank you guys, dsonesuk and justsomeguy Link to comment Share on other sites More sharing options...
divinedesigns1 Posted October 14, 2015 Author Share Posted October 14, 2015 you are checking mime type against extension, 'image/gif' is mime type you need to use pathinfo($target_file,PATHINFO_EXTENSION); to retrieve extension as used here http://www.w3schools.com/php/php_file_upload.asp wooot wooot it working thank you guys, dsonesuk and justsomeguy Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now