Write to File

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
ADTRAN
Forum Newbie
Posts: 10
Joined: Fri Jun 09, 2006 4:39 pm

Write to File

Post by ADTRAN »

Hi,

When I attempt to open a file to write (using fopen("filename.txt","w")), I am unable to open the file.

I'm new to PHP, so here's my question: could this be due to settings on my server (business server, so changning it might be... fun) or is it probably something I'm doing wrong?

I was getting weird results with my main file, so I wrote a small test program:

<html>
<head>
<title>WriteFile Test</title>
</head>
<body>
<?
$fp = fopen("file.txt","w") or die("can't write file");
if (fwrite($fp,$var)) {print "written";}
else {print "not written";}
fclose($fp);
?>
</body>
</html>

And I get "can't write file"...

Any help would be appreciated.

Thanks.
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

a) Make sure the file exists and can be accessed ;)

b) Where is $var defined?
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

c) Is the server platform WIndows/UNIX?

d) Please note in your code you reference 'file.txt' and in your question you reference 'filename.txt'.

e) Is the file in the same directory as the PHP script?

f) PHP Manual: fopen()
ADTRAN
Forum Newbie
Posts: 10
Joined: Fri Jun 09, 2006 4:39 pm

Post by ADTRAN »

Thanks for the suggestions. It was indeed a server thing, and the server admin coperated quite nicely. :D

EDIT: var was defined, I just accidently cut it out when I was cleaning up the code (I tend to test with profanities when something isn't working. :)
Post Reply