You should first test the existence of a file by file_exists().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
try { $fileName = 'uploads/Team/img/'.$team_id.'.png'; if ( !file_exists($fileName) ) { throw new Exception('File not found.'); } $fp = fopen($fileName, "rb"); if ( !$fp ) { throw new Exception('File open failed.'); } $str = stream_get_contents($fp); fclose($fp); // send success JSON } catch ( Exception $e ) { // send error message if you can } |
or simple solution without exceptions:
1 2 3 4 5 6 7 8 9 10 11 12 |
$fileName = 'uploads/Team/img/'.$team_id.'.png'; if ( file_exists($fileName) && ($fp = fopen($fileName, "rb"))!==false ) { $str = stream_get_contents($fp); fclose($fp); // send success JSON } else { // send error message if you can } |