简体   繁体   中英

How to check if a file is empty via ajax call in php?

I want to check if a file exists or not via Ajax call in php.

In php file:

$filename1 = $_FILES['photo']['name']; 

The above line gives an error when the user does not upload the image. But the code is working fine if user does submit the image.

So I did some code to check whether I am receiving the file or not in php:

if (empty($_FILES)) {
  echo 'Image is required.';
}else{
 echo "Image is receiving.";
}

The above code is not working, and the below line shows the error when the user did not submit the image:

Undefined index: photo

Kindly suggest how to check if the image exists or not. Any ideas or suggestions would be welcome.

use isset() function in php

eg:

if (isset($_FILES['photo'])) {
  //do the process
} else {
  // echo error
}

Try below section

if (isset($_FILES['photo']['name']) && $_FILES['photo']['name']!='') {
 // Value Exist
} else {
  // Empty value
}

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