Page 1 of 1

fopen() sesame

Posted: Tue Sep 07, 2004 6:04 am
by mikeb
Hi,

php4
IIS
WinXP

I'm trying to write files using php. I'm using the following idea as a test:

Code: Select all

<?php
$mymessage=fopen("message.txt","w+")or die("can't create file");
fwrite($message,"hello");
fclose($message);
?>
Obviously keeping things as simple as possible until I get to grips with it. Anyway, I keep getting the 'can't create file' message. So my question is:

Do I have to specify a full path for the file as in:
"c:/inetpub/wwwroot/TheWebSite/message.txt"
or a path from the root of the www server in IIS as in:
"/TheWebSite/message.txt"
or backslashes, forward slashes, escaped back/forward slashes? I've been RTFM'ing away without success :(

thanks,

Mike

Posted: Tue Sep 07, 2004 6:07 am
by feyd
the path is related to the file system, not the webroot.

you may need to add t or b onto the end of the flags argument for fopen, since you are on Windows...

Posted: Tue Sep 07, 2004 8:30 am
by mikeb
Thanks Feyd,

Took your advice. This is how my code looks now:

Code: Select all

<?php
$mymessage = fopen("c:/inetpub/wwwroot/MySite/txttest/townlist.txt", "wb")or die("Couldn't create file");
fwrite($mymessage, "hello");
fclose($mymessage);
?>


still not working. I've tried every which way with the path but to no avail!

cheers,

Mike

Posted: Tue Sep 07, 2004 10:27 am
by mikeb
Just re-checked in the php manual. This says that you can use relative paths with the filesystem and that file:// is assumed (since v4.3.0) as the default protocall/wrapper. I've also checked that the permissions on the folder allow me to write and to execute scripts. Anyone any idea why this simple script won't work?

cheers,

Mike

windows

Posted: Tue Sep 07, 2004 11:05 am
by phpScott
I seem to recall from past experience that windows requires some thing silly like

Code: Select all

c:\\dir\\someOtherDir\\file.txt
or double slashes the other way.

It has been away and I not a windows machine at the moment to test it.

I remember the difficulty because I was developing on a windows machine and deploying on LINUX. Yes I have learned the errors of my ways.

Posted: Tue Sep 07, 2004 11:10 am
by timvw
The forward slashes do work, i know because i use them. I'm lazy and don't like typing \\ all the time :) :)


Things that could be a problem (is explained in the manual or installation guide) is that you have to make sure that the user that is running your webserver also has rights to write to that file. I don't know if the IIS_USER has write access on the webroot in a NTFS filesystem by default.

Posted: Wed Sep 08, 2004 4:02 am
by mikeb
thanks people!

phpScott
I'm moving the files to a 'nix box as we speak :-) (I wanted to work on my laptop which has XP installed but I might just forget that idea!)

timvw
I promise I RTFM'd before coming on here (honest!). I made sure that the folder I'm working in was set to allow everyone to do what they want with the folder containing the script (which means you guys probably own my computer by now, lol!) but I'm not good with system permissions on Windows/XP etc. I'm going to move over to the 'nix box momentarily to see what happens

cheers,

Mike

Posted: Wed Sep 08, 2004 4:17 am
by mikeb
mmmmmm Linux!

It's not 10 minutes since I moved the script over to the 'nix box. Script now debugged and working! Coooool! Still like to know why it wouldn't work on XP. I'm gonna look into the user permissions. Now, what's the url of the XP forum?...


cheers,

Mike

Posted: Wed Sep 08, 2004 4:57 am
by timvw
Btw, i think you need write access for your TMP path also...