Jump to content

**solved** Is it in the array?


jbog91

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.