jbog91 Posted April 23, 2006 Share Posted April 23, 2006 I am working on this image upload system. This code works.[code]$allowed = array('images/jpg', 'images/jpeg', 'images/png', 'images/gif', 'images/bmp', 'images/tif', 'images/tiff');// Check various file parameters before allowing to be hostedif ($type != "image/gif"){ echo "Warning : You cannot upload that filetype."; exit;}echo "Uploaded";[/code]But this doesn't.[code]$allowed = array('images/jpg', 'images/jpeg', 'images/png', 'images/gif', 'images/bmp', 'images/tif', 'images/tiff');// Check various file parameters before allowing to be hostedif ($type != $allowed){ echo "Warning : You cannot upload that filetype."; exit;}echo "Uploaded";[/code]How can I see if the file type is in that array? I know it's something simple I'm doing wrong. Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 23, 2006 Share Posted April 23, 2006 Solution is [a href=\"http://ca.php.net/manual/en/function.in-array.php\" target=\"_blank\"]the in_array() function[/a] Quote Link to comment Share on other sites More sharing options...
jbog91 Posted April 23, 2006 Author Share Posted April 23, 2006 Tried using that. How could I use it in that code? I don't won't to add the rest of my information in an if or else statement. All I wont' to do is say, "if the filetype is not in the array, give error."See what I'm saying. I could just use else statements but I want a simple if it's not in there. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 23, 2006 Share Posted April 23, 2006 You use it in your if statement:[code]<?php$allowed = array('images/jpg', 'images/jpeg', 'images/png', 'images/gif', 'images/bmp', 'images/tif', 'images/tiff');// Check various file parameters before allowing to be hostedif (!in_array($type,$allowed)) exit('Warning : You cannot upload that filetype.');echo "Uploaded";?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
jbog91 Posted April 23, 2006 Author Share Posted April 23, 2006 It doesn't work. It just gives the the error message and exits. I had already tried that. Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 23, 2006 Share Posted April 23, 2006 Just wondering where $type comes from, and isn't it more likely that the image type is going to be gif,jpg etc rather than "images/gif" etc. Quote Link to comment Share on other sites More sharing options...
jbog91 Posted April 23, 2006 Author Share Posted April 23, 2006 [code]<?php // This is the file that handles all of the code for uploading and preparing the image// Setting some variable for handling the file$name = $_FILES['imagefile']['name'];$size = $_FILES['imagefile']['size'];$type = $_FILES['imagefile']['type'];$temp = $_FILES['imagefile']['tmp_name'];$newname = date(njyGs);$allowed = array('images/jpg', 'images/jpeg', 'images/png', 'images/gif', 'images/bmp', 'images/tif', 'images/tiff');// Check various file parameters before allowing to be hostedif ($type != "image/gif"){ echo "Warning : You cannot upload that type of file."; exit;}[/code]Hmm, well how shoud this be done? Quote Link to comment Share on other sites More sharing options...
jbog91 Posted April 23, 2006 Author Share Posted April 23, 2006 Nevermind. I got it to work. My orignial code: [code]if (!in_array($type,$allowed)) { echo "Error"; exit;}[/code]which was the same basicaly as what kenrbnsn said worked. In my array, I accidently had images/gif when it was supposed to be image/gif.Thanks anyway yall. Quote Link to comment 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.