fopen() sesame

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
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

fopen() sesame

Post 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
Last edited by mikeb on Wed Sep 08, 2004 9:10 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Post 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
Last edited by mikeb on Wed Sep 08, 2004 9:11 am, edited 1 time in total.
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Post 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
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

windows

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Post 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
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Btw, i think you need write access for your TMP path also...
Post Reply