Page 1 of 1

Write to File

Posted: Wed Jun 14, 2006 1:28 pm
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.

Posted: Wed Jun 14, 2006 1:35 pm
by LiveFree
a) Make sure the file exists and can be accessed ;)

b) Where is $var defined?

Posted: Wed Jun 14, 2006 1:48 pm
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()

Posted: Wed Jun 14, 2006 2:39 pm
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. :)