Jump to content

Archived

This topic is now archived and is closed to further replies.

jbog91

**solved** Is it in the array?

Recommended Posts

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 hosted
if ($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 hosted
if ($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.

Share this post


Link to post
Share on other sites
Solution is [a href=\"http://ca.php.net/manual/en/function.in-array.php\" target=\"_blank\"]the in_array() function[/a]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 hosted
if (!in_array($type,$allowed)) exit('Warning : You cannot upload that filetype.');
echo "Uploaded";
?>[/code]

Ken

Share this post


Link to post
Share on other sites
It doesn't work. It just gives the the error message and exits. I had already tried that.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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 hosted
if ($type != "image/gif"){
    echo "Warning : You cannot upload that type of file.";
    exit;
}
[/code]

Hmm, well how shoud this be done?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.