...

View Full Version : Trying to write to text file



nkline
04-28-2006, 07:47 PM
Greetings,

I'm using Ubuntu Linux v5.10, Apache2, and PHP5.
I am the only user of the computer.

/etc/apache2/sites-available/default.txt is configured to use /var/www/testdir/ as the document root.
I am simply trying to write to a text file located in /var/www/testdir/test.txt

/var/www/testdir/ contains: test.txt and test.php

Here is my PHP script:


<?php

$testfile = "/var/www/testdir/test.txt";
$filehandle = fopen($testfile, "w");

$stringData = "Write this stuff to file\n";
fwrite($filehandle, $stringData);

$stringData = "Then write this stuff to file too\n";
fwrite($filehandle, $stringData);

fclose($filehandle);

?>

Here is the error being returned after the PHP script is ran:


Warning: fopen(/var/www/testdir/test.txt) [function.fopen]: failed to open stream: Permission denied in /var/www/testdir/test.php on line 4

Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/testdir/test.php on line 7

Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/testdir/test.php on line 10

Warning: fclose(): supplied argument is not a valid stream resource in /var/www/testdir/test.php on line 12

I have a feeling this has to do with file permissions and unfortunately I'm a bit of a newbie to Linux. I have not changed any directory or file permissions so they should all be set to the default. I am the only user of this computer.

Any suggestions on getting this to work would be very much appreciated.

Thank you for your time,

*Nick*

Muhammad Haris
04-28-2006, 11:44 PM
Hello nkline!

Right click on the folder testdir and select properties and then "check" read/write permissions for everyone.It should be 777 as in command line language.

I'm not really a user of linux but it should help out as i had used ubuntu for 2 days and tested this kind of stuff.

Regards,
Muhammad Haris

chump2877
04-29-2006, 12:36 AM
try this:


<?php

$testfile = "/var/www/testdir/test.txt";
chmod($testfile, 0777);
$filehandle = fopen($testfile, "w");

$stringData = "Write this stuff to file\n";
fwrite($filehandle, $stringData);

$stringData = "Then write this stuff to file too\n";
fwrite($filehandle, $stringData);

fclose($filehandle);

?>

Muhammad Haris
04-29-2006, 12:42 AM
try this:


<?php

$testfile = "/var/www/testdir/test.txt";
chmod($testfile, 0777);
$filehandle = fopen($testfile, "w");

$stringData = "Write this stuff to file\n";
fwrite($filehandle, $stringData);

$stringData = "Then write this stuff to file too\n";
fwrite($filehandle, $stringData);

fclose($filehandle);

?>

You do need to chmod the txt file but it is important to chmod the directory first!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum