...

View Full Version : Check if a text file exists and if not create it



Dalziel
04-04-2003, 11:16 AM
I want to use a PHP script to check whether a file exists or not and if not to create it.

Example that is not written in PHP but shows what I want to do:

if (main.txt exists)
{
do nothing
}
else
{
create main.txt with just one character inserted into it, a 0.
}

Weirdan
04-04-2003, 12:06 PM
clearstatcache();
if(!file_exists("your_filename_here"))
{
$fp = fopen("your_filename_here","w");
fwrite($fp,"0");
fclose($fp);
}


Don't forget to add error checking.

brothercake
04-04-2003, 12:15 PM
Doesn't fopen automatically create a file if the named file doesn't exist?

Weirdan
04-04-2003, 12:19 PM
Originally posted by brothercake
Doesn't fopen automatically create a file if the named file doesn't exist?
Yes, but it also truncate file. It may be not what Dalziel wants.

Dalziel
04-04-2003, 12:23 PM
Yes but it's being used for a hit counter, if I left it for fopen to create it, the first time the page was loaded an error would be displayed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum