parksobong Posted August 29, 2006 Share Posted August 29, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/19051-why-doesnt-fopen-with-w-automatically-create-file/ Share on other sites More sharing options...
ober Posted August 29, 2006 Share Posted August 29, 2006 try 'w+'. Quote Link to comment https://forums.phpfreaks.com/topic/19051-why-doesnt-fopen-with-w-automatically-create-file/#findComment-82382 Share on other sites More sharing options...
AndyB Posted August 29, 2006 Share Posted August 29, 2006 fopen with the w or w+ parameter [i]attempts to[/i] create the file if it doesn't exist. I doubt it would unless the folder write permissions were set correctly. Quote Link to comment https://forums.phpfreaks.com/topic/19051-why-doesnt-fopen-with-w-automatically-create-file/#findComment-82385 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.