简体   繁体   中英

Image upload validation in PHP in registration form

I need help with checking if an image has been uploaded and if it has not, set the image path to a default one (Nopic.png). I am using a form to register users and I need to set a profile image for them. The form works ok and the image upload works ok (using w3c's code). But how do I check if the file field is empty or not and set the path accordingly?

After upload you can check whether the file exists in the expected directory or not and if not use default image:

$image = "../uploads/foo/image.png"; //path to your uploaded image
if(file_exists($image) && (is_file($image))){
    //if exists show the image
}else{
    $image = "../uploads/foo/default.png"; //path to your default image
    //you can then show this image
}

Use is_uploaded_file()

if(!file_exists($_FILES['myfile']['tmp_name']) || !is_uploaded_file($_FILES['myfile']['tmp_name'])) {
echo 'No upload';
}

Here is the php documentation

http://php.net/manual/en/function.is-uploaded-file.php

check the file if it exists:

    if (file_exists ($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']) ) {
        //yes it exist
    }else{
       //default one (Nopic.png)
   }

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