Another random guess, maybe you should check the HTTP header/response/type of content...etc
Maybe httpstatus.io or anything similar can help or maybe a hits
I can only random guess about the issue,
- is it file permission stopping the code to open it?
- file is not empty, right?
As the error is open fail, I believe it stopped at second line and not even touched the rest of code