Kyle Morgan
Kyle Morgan

Reputation: 680

Error with my upload file script (PHP)

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!

Upvotes: 0

Views: 163

Answers (1)

Stefan Fandler
Stefan Fandler

Reputation: 1141

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

Upvotes: 6

Related Questions