Page 1 of 1

fopen() creating file

Posted: Sat Jan 31, 2004 8:11 pm
by d3ad1ysp0rk
php.net wrote:'a+' | Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
http://us2.php.net/function.fopen

Code: Select all

$filename = "counter.txt"; //the name of the text file which stores the info
$handle = fopen($filename, "a+");
$contents = fread($handle, filesize($filename));
my page wrote:Warning: fopen(counter.txt): failed to open stream: Permission denied in /home/des404/public_html/counter.php on line 7

Warning: filesize(): Stat failed for counter.txt (errno=2 - No such file or directory) in /home/des404/public_html/counter.php on line 8

Warning: fread(): supplied argument is not a valid stream resource in /home/des404/public_html/counter.php on line 8
Is there something else I have to do to allow fopen to create the file? I want to keep this script to as simple installation as possible (ie. making them upload 1 file instead of 2 being ideal :P )

Thanks

Posted: Sat Jan 31, 2004 8:23 pm
by Illusionist
are you using a hosting company? If so you may not have file open abilites, because that workd fine for me!

Posted: Sat Jan 31, 2004 8:25 pm
by d3ad1ysp0rk
I can fopen things.. maybe they just disallowed creation of files.. :(

and this is meant for anyone.. so I guess I'll have to have them upload and chmod counter.txt too..

Posted: Sat Jan 31, 2004 9:59 pm
by DuFF
Does this work?

Code: Select all

<?php
$filename = "counter.txt"; //the name of the text file which stores the info
if(file_exists($filename))
{
    $handle = fopen($filename, "a+");
    $contents = fread($handle, filesize($filename));
}
else
{
    touch($filename); // Create blank file
    chmod($filename,0777); //chmod it
    $handle = fopen($filename, "a+");
    $contents = fread($handle, filesize($filename));
}
?>

Posted: Sat Jan 31, 2004 10:29 pm
by d3ad1ysp0rk
Warning: touch(): Unable to create file counter.txt because Permission denied in /home/des404/public_html/counter.php on line 14

Warning: chmod(): No such file or directory in /home/des404/public_html/counter.php on line 15

Warning: fopen(counter.txt): failed to open stream: Permission denied in /home/des404/public_html/counter.php on line 16

Warning: filesize(): Stat failed for counter.txt (errno=2 - No such file or directory) in /home/des404/public_html/counter.php on line 17

Warning: fread(): supplied argument is not a valid stream resource in /home/des404/public_html/counter.php on line 17