fopen() creating file

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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

fopen() creating file

Post 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
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

are you using a hosting company? If so you may not have file open abilites, because that workd fine for me!
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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..
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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));
}
?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

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