0

I have a script:

if(isset($_FILES['image'])){
$errors = array();
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$file_name = $_FILE['image']['name'];
$file_ext = strtolower(end(explode('.', $file_name)));
$file_size = $_FILE['image']['size'];
$file_tmp = $_FILE['image']['tmp_name'];

if(in_array($file_ext, $allowed_ext) === false){
    $errors[] = 'Extension not allowed';
}

if($file_size > 2097152){
    $errors[] = 'file size must be under 2mb';
}

if(empty($errors)){
    if(move_uploaded_file($file_tmp, "../../img/usr/profile/.$file_name")){
        echo 'File uploaded';
    }
}else{
    foreach($errors as $error){
        echo $error, '<br>';
    }
}
}

And it's not working. It is supposed to upload an image to a certain directory. Here's the html:

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="image">
    <input type="submit" name="submit" value="Update Info">
</form>

When I click submit, there is an error at the top of the screen saying 'Extension not allowed'. The html and php is in the same file (I just gave you a small snippet.) Does anything look wrong with my code? thanks!

1
  • This error appears if you choose .jpg .jpeg .png or .gif too? Commented Oct 8, 2012 at 3:46

1 Answer 1

6

To access the files, you have to use the $_FILES variable. In your code, you have sometimes used $_FILE, which does not work ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.