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.';
 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.

2 answers

  • answered 2018-11-08 07:09 Shobi

    use isset() function in php


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

  • answered 2018-11-08 07:16 VinothRaja

    Try below section

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