0

I am displaying contents from the text file from database through file_get_contents.

$sql = "SELECT * FROM myContent";
$query = mysqli_query($db, $sql);
$row = mysqli_fetch_row($query);


$file = file_get_contents('mytext.txt');
echo $file;

Everything fine, but when my file is missing I get errors. How I put if-else condition in my php code when file is missed or not in place. Thank you.

2 Answers2

1
if(file_exists('mytext.txt')) {
    $file = file_get_contents('mytext.txt');
    echo $file;
}
bars
  • 107
  • 5
  • This is the most appropriate way to handle this thing. As file_get_contents doesn't throw an exception but throw an error, so it cannot be handle by try catch – JessGabriel Oct 02 '19 at 11:50
-1

Hope this helps ;)

if(!empty(file_get_contents('mytext.txt'))):
    // Display file contents
else:
   // Error handling
endif;
Gerald Mathabela
  • 475
  • 2
  • 4
  • 16