PHP Cheat Sheet: File Handling

1. Checking Whether a File Exists: if (file_exists(“testfile.txt”)) echo “File exists”; 2. Reading from Files: Method 1: Using fgets() to read one line a time: $fh = fopen(“testfile.txt”, ‘r’) or die(“Cannot open file.”); while (!feof($fh)) {     $line = fgets($fh);     $out .= $line; } fclose($fh); Method 2: Using file_get_contents() to read the entire content: […]