July 5, 2010

PHP file read not working

Question by Bharanikumar

On a server I have the file some.php. I try to read the contents using file_get_content and fread. Both these don’t read my content.

If I try some other URL like yahoo.com, these functions are reading the contents. Why does this happen?

$my_conetn =  file_get_contents("http://example.com");
echo $my_conetn;

The above snippet is not working.

$handle = fopen($filename, "r");
$contents = fread($handle, 20);
echo $contents;
fclose($handle);

Also the above snippet is not working.

How to check my server? Is there any file read operation locked or not?

edit: I am not sure, but it is only my server file that can’t be read. I can read the other server links. So I contact my hosting provider, then I get back to you guys/gals.

Answer by ThiefMaster

When using file functions to access remote files (paths starting with http:// and similar protocols) it only works if the php.ini setting allow_url_fopen is enabled.
Additionally, you have no influence on the request sent. Using CURL is suggested when dealing with remote files.

If it’s a local file, ensure that you have access to the file – you can check that with is_readable($filename) – and that it’s within your open_basedir restriction if one is set (usually to your document root in shared hosting environments).

Additionally, enable errors when debugging problems:

ini_set('display_errors', 'on');
error_reporting(E_ALL);

Answer by Starx

Try to display your PHP configuration with phpinfo() and see if something is blocking you to access the file.

And first try to access your local file, rather than one hosted?

Check:

allow_url_fopen

display_errors

Have you tried on the local file?

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!