PDA

View Full Version : Problem opening textfile for writing


mr breaker
03-14-2004, 11:01 PM
I'm trying to open a text file for writing but am getting this error:

Warning: fopen(cgi-bin/counter.txt): failed to open stream: line 8

Warning: fputs(): supplied argument is not a valid stream resource line 9

Warning: fclose(): supplied argument is not a valid stream resource line 10


here is my code:


$counter_file = ("cgi-bin/counter.txt");
$visits = file($counter_file);
$visits[0]++;

line 8: $fp = fopen($counter_file , "w");
line 9: fputs($fp , "$visits[0]");
line 10: fclose($fp);


Is there something wrong with my code, or do I need different permissions to write to a file?

y6y6y6
03-15-2004, 10:26 AM
To write to a file this way the file must be owned by the website user, usually "nobody".

This would be much easier to do with a database.

Viper007Bond
03-15-2004, 05:23 PM
Indeed, using flatfile for data storage isn't a good idea or efficient one for that matter either.

geroditus
03-17-2004, 07:56 AM
You also have to make sure the path to the file is correct; you are getting that error because it isn't. Remember, the path is from where the page is located; if it's in /myfolder/, then you have to use ../cgi-bin/counter.txt for the file path. Better yet, use /cgi-bin/counter.txt to be able to access the counter file from anywhere.

geroditus
03-17-2004, 07:58 AM
You should also check $fp to make sure it is valid. Maybe use:
$fp = fopen($counter_file , "w");
if(!$fp)
{
die("Could not open file for writing.");
}
//Now the code to write to and close the file