fwrite() - Trouble getting this to work - Newbee

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
greenAlien
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2003 10:08 pm

fwrite() - Trouble getting this to work - Newbee

Post 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 ?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
greenAlien
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2003 10:08 pm

Post 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?
greenAlien
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2003 10:08 pm

Post 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 );
?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

And you don't get see the die() output?
greenAlien
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2003 10:08 pm

Post 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()
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
derek
Forum Newbie
Posts: 17
Joined: Sat Aug 16, 2003 11:31 am

Post 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.
greenAlien
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2003 10:08 pm

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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 :(
Post Reply