①fread (', ' W ') calls the file to be created when the file does not exist, where the parameter uses fread ("'," w ") to cause the file to be created, and the operation is correct after the single quote is modified.
② a new log file in the project, you need to determine if the log file exists
When you use File_exists to judge a file, the file is deleted, and the file_exists or is_file determines that it still returns true.
View the manual about file_exists has the following comments
Comments
Note: The result of this function is cached. See Clearstatcache () for more details.
View Clearstatcache ()
Description void
clearstatcache ([ bool
$clear_realpath_cache
= False [, string
$filename
]])
When using stat (), lstat () or any of the functions listed in the affected function table (see below), PHP caches the return information for these functions to provide faster performance. However, in some cases, you may want to clear the cached information. For example, if you check the same file multiple times in a script and the file is at risk of being deleted or modified during this script execution, you need to clear the file state cache. In this case, you can use the clearstatcache () function to clear the file information that is cached by PHP.
It is important to note that PHP does not cache its information for files that do not exist. So if you call file_exists () to check for a nonexistent file, it will be returned FALSE
before the file is created. If the file TRUE
is created, it will return the function unlink () and will automatically clear the cache , even if it is deleted later.
This function caches information for a specific file name, so you need to call Clearstatcache () only if you have multiple operations on the same file name and require that the file information not be cached.
void Clearstatcache ([ bool $clear_realpath_cache
= False [, string $filename
]])
2$date = Date ('Ym', Time ());3$dir = Str_replace ('\\','/', (DirName (DirName (dirname (__file__)))."/control/wxpay/log/notify_". $date.". txt"));4 //Clear File Cache5 Clearstatcache (TRUE, $dir);6 if(File_exists ($dir)) {7$fh = fopen ($dir,'a');8}Else{9$fh = fopen ($dir,'W');Ten } One A Flock ($FH, lock_ex); -Fwrite ($FH,"date of execution:". Date ("y/m/d h:i:s", time ())."\ n". $contents."\ n"); - Flock ($FH, lock_un); the fclose ($FH); -
Fread () Create File and file_exists () file cache issues