Page 1 of 1

fwrite() - Trouble getting this to work - Newbee

Posted: Sun Aug 17, 2003 10:08 pm
by greenAlien
Hello,
I'm a kook with php.
I decided to snip ASP and go out into the big world of open source.
I'm on a windows machine using 2000 server. IIS without any permissions.

I am trying to get fwrite() to work, but it won't. It seems I can't access the file to write to it. Is there a setting in php.ini that I am missing?
Here's my code:
(It dies at line 4.)

1. <?php
2. $filename = "test.txt";
3. print "Writing to $filename<br>";
4. $fp = fopen( $filename, "w" ) or die("Could not open $filename");
5. fwrite( $fp, "Hello world\n" );
6. fclose( $fp );
7.
8. print "Appending to $filename<br>";
9. $fp = fopen( $filename, "a" ) or die("Couldn't open $filename");
10. fputs( $fp, "And another thing\n" );
11. vfclose( $fp );
12 ?>

Posted: Sun Aug 17, 2003 10:26 pm
by nigma
first, you have a typo near the end where you have vfclose($fp)

Also, in order to write to the file you must create it first, unless you open it in append mode.

Posted: Sun Aug 17, 2003 11:15 pm
by greenAlien
actually the typo came when I transposed it from my editor, and the file test.txt exists in the same directory. Anything else?

Posted: Sun Aug 17, 2003 11:17 pm
by greenAlien
<?php
$filename = "test.txt";
print "Writing to $filename<br>";
$fp = fopen( $filename, "w" ) or die("Could not open $filename");
fwrite( $fp, "Hello world\n" );
fclose( $fp );
print "Appending to $filename<br>";
$fp = fopen( $filename, "a" ) or die("Couldn't open $filename");
fputs( $fp, "And another thing\n" );
fclose( $fp );
?>

Posted: Sun Aug 17, 2003 11:57 pm
by nigma
And you don't get see the die() output?

Posted: Mon Aug 18, 2003 12:01 am
by greenAlien
This is what I get"

Writing to test.txt
Could not open test.txt


Notice it says "Could not". That's the first die()
"Couldn't" is the second die()

Posted: Mon Aug 18, 2003 12:44 am
by nigma
Yea, that is real real wierd. I cannot think of anything other than permissions that could cause that problem. Try getting rid of everything but the:

Code: Select all

$filename = "test.txt";

$fp = fopen($filename, "a") or die ("Couldn't open $filename");

// Rest of code that should write to file "test.txt"
If that errors out (Couldn't open test.txt) that my bet is that the file permissions need to be changed.

Posted: Mon Aug 18, 2003 1:17 am
by derek
I'm agree with nigma and the code is absolutely right. I'm familiar with windows platform and I think you have user Permissions problem. If you are in LAN then talk to admin for getting permissions. If you have rights to access the IIS then follow me: Open the IIS console select your domain or host name, right click on it and select the properties and there is a tab with write, read, execute etc permissions. Make sure to select the read,write and other permissions which you want. And let me know it works for you.

Posted: Mon Aug 18, 2003 1:19 am
by greenAlien
Too funy! I actually already tried that as well. It didn't work either; but I think I know what it is now...

I transfered the all the files to another machine and the code worked perfectly.

I then began to compare the differences.
The server on both machines (IIS 5) is setup identical.

I am using the same php.ini file on both machines.

The only difference I could find is in phpinfo.php under the security profile.

Example of phpinfo.php settings in fautly machine:
ALLUSERSPROFILE C:\Documents and Settings\All Users

Example of phpinfo.php settings in working machine:
ALLUSERSPROFILE C:\Documents and Settings\All Users.WINNT

My question now is; how do I access these parameters that are displayed in phpinfo.php?

I can't find them in php.ini.

Thanks,
Rob

Posted: Mon Aug 18, 2003 1:19 pm
by nigma
This is where I have to take a step back and admit that I can be of no more assistance to you. Sorry :(