Jump to content

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


Recommended Posts

The following is my hit-counter script:
[code]
                $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)
[/code]

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?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.