I'm using file_get_contents()
along with file_put_contents()
to download some files.
I'm using as everyone suggests:
$file = file_get_contents('https://www.example.com/image.jpg');
if ($file === false) { // do stuff on failure }
It works flawlessly but the thing is that today I just realized that some files have been deleted or something but still the response code is 200 so file_get_contents()
returns empty string which means "" === false // is false
so it won't execute that code in that if
condition.
My question is how to properly write that if
condition to also check for an empty file?
My suggestions are:
if ($file == false) { // do stuff on failure or empty }
if ($file === false || strlen($file) === 0) { // do stuff on failure or empty }
if ($file === false || empty($file) { // do stuff on failure or empty }
PS For now I am only interested in suggestions for file_get_contents()