locoputo Posted April 28, 2010 Share Posted April 28, 2010 I have this section in a script of mine: @$info= fopen('/home/myuser/info.txt','r'); if (!$info) { echo "[-] Error : coudn't read /home/myuser/info.txt"; exit; } this always ends as Couldn't read. So I tried only running fopen('/home/myuser/info.txt','r'); and I got a blank response. So I tried changing the function to readfile('/home/myuser/info.txt','r'); That seemed to work, the file was read. Now I changed that in my script and next thing I know I get this error: Warning: feof(): supplied argument is not a valid stream resource in /path/to/my/script.php on line 17 Warning: fgets(): supplied argument is not a valid stream resource in /path/to/my/script.php on line 18 Line 17 and 18 in the script contain the following: while(!feof($info)) { $str=fgets($info); This will be because for feof to work it must point to a file successfully opened by fopen(). Would any of you expert coders be able to suggest a replacement for this code, so I can use readfile() instead? Would anyone know any other way to execute this code by using an alternative function to read the file? I would greatly appreciate any help. Thank you all in advance. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted April 28, 2010 Share Posted April 28, 2010 I prefer to use fopen, read the data, and make sure there isn't any errors from there. /* Define the file, so we can use the name to check size of the file in the future. Much faster when dealing with a large number of files */ $file = "/home/myuser/info.txt"; /* Open the file for reading */ $fh = fopen($file, "r"); /* Read the file, in its entirety */ $data = fread($fh, filesize($file)); /* If reading the file failed, or if it is empty, throw an error. If not, move on */ if (!$data || empty($data)){ echo "[-] Error : couldn't read $file"; exit(); } /* do whatever you are planning on doing from here */ /* Close the file */ fclose($fh); Quote Link to comment Share on other sites More sharing options...
locoputo Posted April 28, 2010 Author Share Posted April 28, 2010 Does that mean you could rewrite that section of the code, allowing me to use another function instead of fopen()? If so, could you guide me please? Quote Link to comment Share on other sites More sharing options...
katierosy Posted April 29, 2010 Share Posted April 29, 2010 I am able to read it. In your case please see that file path is correct <? @$info= fopen('test.txt','r'); if (!$info) { echo "[-] Error : coudn't read /home/myuser/info.txt"; exit; } print fread($info,filesize('test.txt')) ?> Quote Link to comment Share on other sites More sharing options...
loco41211 Posted April 29, 2010 Share Posted April 29, 2010 Thank you for the help everybody. I am happy to say that the problem has been fixed. It wasn't todo with the fopen function, I made a stupid mistake at a later section which is why it wouldn't work properly on my test server. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.