Jump to content


Photo

**solved** Is it in the array?


  • Please log in to reply
7 replies to this topic

#1 jbog91

jbog91
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 23 April 2006 - 12:08 AM

I am working on this image upload system.

This code works.

$allowed = array('images/jpg', 'images/jpeg', 'images/png', 'images/gif', 'images/bmp', 'images/tif', 'images/tiff');

// Check various file parameters before allowing to be hosted
if ($type != "image/gif"){
    echo "Warning : You cannot upload that filetype.";
    exit;
}

echo "Uploaded";

But this doesn't.

$allowed = array('images/jpg', 'images/jpeg', 'images/png', 'images/gif', 'images/bmp', 'images/tif', 'images/tiff');

// Check various file parameters before allowing to be hosted
if ($type != $allowed){
    echo "Warning : You cannot upload that filetype.";
    exit;
}

echo "Uploaded";

How can I see if the file type is in that array? I know it's something simple I'm doing wrong.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 23 April 2006 - 12:24 AM

Solution is [a href=\"http://ca.php.net/manual/en/function.in-array.php\" target=\"_blank\"]the in_array() function[/a]
Legend has it that reading the manual never killed anyone.
My site

#3 jbog91

jbog91
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 23 April 2006 - 12:29 AM

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.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 April 2006 - 12:58 AM

You use it in your if statement:
<?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 hosted
if (!in_array($type,$allowed)) exit('Warning : You cannot upload that filetype.');
echo "Uploaded";
?>

Ken

#5 jbog91

jbog91
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 23 April 2006 - 01:11 AM

It doesn't work. It just gives the the error message and exits. I had already tried that.

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 23 April 2006 - 01:15 AM

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.
Legend has it that reading the manual never killed anyone.
My site

#7 jbog91

jbog91
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 23 April 2006 - 01:23 AM

<?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 hosted
if ($type != "image/gif"){
    echo "Warning : You cannot upload that type of file.";
    exit;
}

Hmm, well how shoud this be done?

#8 jbog91

jbog91
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 23 April 2006 - 01:40 AM

Nevermind. I got it to work. My orignial code:

if (!in_array($type,$allowed)) {
        echo "Error";
        exit;
}

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users