简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM