简体   繁体   中英

Php problem checking file upload value

Ok this might be a simple problem but I cannot figure this out at all.... I am trying to check if the user is trying to upload a file, upload it else execute some other code, but:

if(isset($_FILES['rv_img']['name']) && !empty($_FILES['rv_img']))
        {
            echo "here ";print_r($_FILES);
                }
else
                {
                        echo "no file uploaded";
                }

I want to see "no file uploaded" cause I am submitting the form with blank file input field.. but instead I am getting:

here Array ( [rv_img] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) [thumb_img] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) ) 

try this

if ( isset($_FILES["file"]) && $_FILES["file"]["error"]==0 )
{
  $file_name= $_FILES["file"]["name"];
  move_uploaded_file($_FILES["file"]["tmp_name"], YOUR PATH TO STORE FILE. $_FILES["file"]["name"]);
}
else
echo("No file uploaded");
if(isset($_FILES['rv_img']['name']) && !empty($_FILES['rv_img']['name'])) 

That's because !empty($_FILES['rv_img']) will always return true. Try with !empty($_FILES['rv_img']['name']) .

You get the error code 4, which is UPLOAD_ERR_NO_FILE constant. All the file upload error constants can be seen at http://php.net/manual/en/features.file-upload.errors.php . Try checking this condition:

if (... && $_FILES['rv_img']['error'] == UPLOAD_ERR_OK) { ...

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