Warning feof () expects parameter 1 to be a resource

My error logs get out of hand with the two errors below

warning feof() expects parameter 1 to be resource 

and

 warning fread() expects parameter 1 to be resource 

Code bit responds

 <?php $file = '../upload/files/' . $filex; header("Content-Disposition: attachment; filename=" . urlencode($file)); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Description: File Transfer"); header("Content-Length: " . filesize($file)); flush(); // this doesn't really matter. $fp = fopen($file, "r"); while (!feof($fp)) { echo fread($fp, 65536); flush(); // this is essential for large downloads } fclose($fp); ?> 

I used this code to download the headers, but freaking it right now - before anyone asks what I tried, I tried Google, but still do not understand the error message.

+4
source share
2 answers

fopen exits and returns false. false is not a resource, so a warning.

It is better to check $ fp before entering it as a resource-like argument:

 if(($fp = fopen($file, "r"))) { [...] } 
+4
source

I recently ran into this problem. The code worked fine in my local environment. But when it was uploaded to the server, I received the message discussed in this thread. In the end, the problem was that the server is case sensitive in the file names, but my local environment is not. After correcting the file name, everything started to work.

0
source

Source: https://habr.com/ru/post/1480805/


All Articles