cancel
Showing results for 
Search instead for 
Did you mean: 

PHP fopen()

N/A

PHP fopen()

I have a little script which uses fopen() to open a text file and read the contents into a string. It works perfectly if the file is on my cgi space. Doesn't matter where it is on my cgi space I can refer to it by it's relative path, the script finds it and process it correctly.

The problem arises if I store it elsewhere. It simply doesn't find it. According to http://cgi.free-online.net/phpinfo.php allow_url_open is set to "on" and yet if I try to refer to my file using it's URL on my web space it doesn't find it. File permissions are chmod777 so it's not that.

Any ideas anyone?
6 REPLIES
N/A

Re:PHP fopen()

I take it nobody reads these forums then...

...oh well.
Community Veteran
Posts: 1,699
Registered: 30-07-2007

Re:PHP fopen()

Hi

I read them, I just don't know ANYTHING about CGI scripting, so couldn't answer Shockedops:

John
N/A

PHP fopen()

I think what you are trying to do should work. I am sure that I have got it to work that way in the past. In fact you can do it to have fopen() read off your home drive if you put your own IP number into the address.
N/A

Re: PHP fopen()

I wonder if anyone else has any ideas on this.

I've drawn up two very simple test scripts one which picks up the file from the CGI space and the other from my space on the homepages server. The two are as follows...

http://cgi.gmsmalley.free-online.co.uk/fopentest.php

Quote
<html>
<head>
</head>
<body>
<?php
$file = "test.txt";
$link = fopen($file, "r");
$contents = fread($link, filesize($file));
fclose($link);
echo $contents;
?>
</body>
</html>


which picks up the file from the CGI space OK and the other...

http://cgi.gmsmalley.free-online.co.uk/fopentest2.php

Quote
<html>
<head>
</head>
<body>
<?php
$file = "http://www.gmsmalley.free-online.co.uk/test.txt";
$link = fopen($file, "r");
$contents = fread($link, filesize($file));
fclose($link);
echo $contents;
?>
</body>
</html>


which fails to pick up the file on my homepages server.

As mentioned above there seems no reason why this should fail as FOL state that url_open is allowed.

Have I made an error in the PHP?

If url_open is not allowed does anyone know the relative path of the homepages server from the cgi server?
N/A

Re: PHP fopen()

Quote
Have I made an error in the PHP?

Yes. filesize() does not work on remote files so it won't work.

You do realise you could just use file_get_contents() which returns the file in a string to save you doing all that Smiley

This means that
<html>

<head>
</head>
<body>
<?php
$file = "http://www.gmsmalley.free-online.co.uk/test.txt";
$link = fopen($file, "r");
$contents = fread($link, filesize($file));
fclose($link);
echo $contents;
?>
</body>
</html>
would become
<html>

<head>
</head>
<body>
<?php
$file = "http://www.gmsmalley.free-online.co.uk/test.txt";
$contents = file_get_contents($file);
echo $contents;
?>
</body>
</html>

Quote
If url_open is not allowed does anyone know the relative path of the homepages server from the cgi server?
There isn't one.
N/A

Re: PHP fopen()

I knew someone would know the answer.

Thanks for your response, you code works perfectly. Cheers Smiley