Jump to content


Photo

Why doesn't fopen() with 'w' automatically create file?


  • Please log in to reply
2 replies to this topic

#1 parksobong

parksobong
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 29 August 2006 - 06:54 PM

The following is my hit-counter script:
                $cnt_file = "counter.dat";

                if($fp = @fopen($cnt_file, 'r')) {
                        $count = fread($fp, filesize($cnt_file));
                        fclose($fp);
                }
                else
                        $count = 0;

                $count = $count + 1;

                $fp = fopen($cnt_file, 'w');
                fwrite($fp, $count);

                print("You are visitor number " . $count);

                fclose($fp)

First, I didn't have a file called "counter.dat," and I assumed that this script would automatically create a "counter.dat" file if non-existent.

However, "fopen()" errors continued to appear until I manually created "counter.dat" with a value of n.

By nature, doesn't fopen($file, 'w') automatically create $file if it doesn't exist?

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 29 August 2006 - 06:56 PM

try 'w+'.

Info: PHP Manual


#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 29 August 2006 - 06:59 PM

fopen with the w or w+ parameter attempts to create the file if it doesn't exist. I doubt it would unless the folder write permissions were set correctly.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users