fopen won't work with url but works with server path

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chemoul
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2005 6:37 pm

fopen won't work with url but works with server path

Post by chemoul »

Hi,

I have a problem with the following code:

Code: Select all

$myFile = $siteurl."feed.xml";
echo "myfile: $myFile";
$fh = fopen($myFile, 'w') or die("can't open file");

fwrite($fh, $feeddata);

fclose($fh);
If $siteurl is a URL, such as http://www.myserver.com/feeddirectory/, then I get the error "can't open file". The URL that is echoed by the script is correct, if I copy and paste the URL in my webbrowser it displays the xml file.
The same script works if I use the local path on the server instead, ie. /usr/home/feeddirectory/, but that's not really an option for my script.
allow_url_fopen is set to 'on' on the server. What's more, the same problem occurs on my local testserver (windows xp) and on my remote server (linux). The above php code and the feed.xml file are in different directories on the server but I don't really see how this could cause the problem.

Does anyone have any ideas, because this is starting to drive me nuts.

Thank you for your replies.

Stefan
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

url wrappers are off it sounds..

http://php.net/ref.filesystem#ini.allow-url-fopen
chemoul
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2005 6:37 pm

Post by chemoul »

as I said

"allow_url_fopen is set to 'on' on the server"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

still sounds like it's actually off.. you may want to check if http is a registered stream
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

http://php.belnet.be/manual/en/wrappers.http.php says:
Allows read-only access to files/resources via HTTP 1.0, using the HTTP GET method

So you should consider using FTP(S)/SCP if you want to write.
chemoul
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2005 6:37 pm

Post by chemoul »

wow, this is turning into a chat.. thank you for your prompt responses :-)
The information that the url wrapper is on is taken from phpinfo, where it says

Configuration, PHP Core

Diretcive Local Value Master Value
allow_url_fopen On On

How do I check whether http is a registered stream?

Thank you for your help.
chemoul
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2005 6:37 pm

Post by chemoul »

Hi Timvw

your resource actually says "HTTP connections are read-only; you cannot write data or copy files to an HTTP resource."

Bugger...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

damn.. I completely missed the "write" request.. :lol:

:oops:
chemoul
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2005 6:37 pm

Post by chemoul »

But then I get the error msg during fopen and not when I'm trying to write to the file...
Admittedly, this won't solve my problem with writing to the file, but why won't it even open the file?

... ok... the 'w' flag in fopen..
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

You are asking to open the file in "write mode" which has to erase the file first. So if you don't have write capability, you can't even open the file. If you want to test remote opening, change the 'w' to a 'r' to see if the open succeeds.
Post Reply