Muhammad Arif
Muhammad Arif

Reputation: 1022

How to check if a user uploaded a file using PHP?

I made a PHP page and I want to check if a user selected a file and CSV file checkbox is checked no error occurs. I used the following code:

 $file = $fu['filepath'].$fu['filename'];
        $handle=fopen($file,"r");
        while($row = fgetcsv($handle, 1024)){
 echo "<input type='checkbox' name='receptionts[]' checked='checked' value='".$row[1] ."' /> ".$row[0]." <br />";
        }

When user does not select a file for upload it gives errors, see below:

Warning: fopen() [function.fopen]: Filename cannot be empty
Warning: fgetcsv() expects parameter 1 to be resource, boolean given 

Upvotes: 0

Views: 124

Answers (1)

Sandeep Kumar
Sandeep Kumar

Reputation: 288

Try this:

$file = $fu['filepath'].$fu['filename'];
$handle=@fopen($file,"r");
if($handle) {
  while($row = fgetcsv($handle, 1024)){
    echo "<input type='checkbox' name='receptionts[]' checked='checked' value='".$row[1] ."' /> ".$row[0]." <br />";
  }
} else {
  // File doesn't exist. do something.
}

Upvotes: 1

Related Questions