Php deletes the file and recreates it. the file creation time (filectime) remains unchanged. how can this problem be solved ??

Source: Internet
Author: User
The created object uses filectime to obtain the created time t2. why t1t2 ?? Is to create...
  1. Php creates a file named test.html and obtains the creation time t1 using filectime.

  2. Php deletes a file named test.html

  3. Php re-creates a file named test.html, and obtains the creation time T2.

Why t1 = t2 ?? Why is the creation time not updated ?? I have used the clearstatcache function to clear the file status cache. how can this problem be solved ?? (It is important to know the creation time of the cached file when determining whether to regenerate the cached file ~)
Below is the code for this problem (a bit confidential, please be patient ):

// Page cache if (defined ('debug _ mode ')&&! DEBUG_MODE) & (defined ('cache _ control') & CACHE_CONTROL) {$ opr = $ GLOBALS ['module']. '/'. $ GLOBALS ['platform']. '/'. $ GLOBALS ['controller']. '/'. $ GLOBALS ['AC']; $ cache_name = md5 ($ opr ). TEMPLATE_SUFFIX; $ cache_file = APP_DIR. '/'. $ GLOBALS ['module']. '/'. $ GLOBALS ['platform']. '/Cache /'. $ cache_name; // clear the file state cache (in my usage, it is useless at all ..., what is the situation ??) Clearstatcache (); // The cache file does not exist. create if (! $ GLOBALS ['fopr']-> checkFile ($ cache_file) {require_once $ view_file; $ GLOBALS ['fopr']-> cFile ($ cache_file ); $ GLOBALS ['fopr']-> wData ($ cache_file, ob_get_contents ();} else {// time-out and re-cache if (time ()> filectime ($ cache_file) + CACHE_LIFE_TIME) {require_once $ view_file; // when the cache file times out, delete the original cached file $ GLOBALS ['fopr']-> dFile ($ cache_file ); // create a cache file with the same name // (when a file is created, the filectime file creation time has not been updated! How can this be solved ?? // As a result, I cannot regenerate the cache file !) $ GLOBALS ['fopr']-> cFile ($ cache_file); // write cache $ GLOBALS ['fopr']-> wData ($ cache_file, ob_get_contents (), 'W');} else {// load the cached file require_once $ cache_file;} else {// display the Dynamic File (non-cache) require_once $ view_file ;}

Reply content:
  1. Php creates a file named test.html and obtains the creation time t1 using filectime.

  2. Php deletes a file named test.html

  3. Php re-creates a file named test.html, and obtains the creation time T2.

Why t1 = t2 ?? Why is the creation time not updated ?? I have used the clearstatcache function to clear the file status cache. how can this problem be solved ?? (It is important to know the creation time of the cached file when determining whether to regenerate the cached file ~)
Below is the code for this problem (a bit confidential, please be patient ):

// Page cache if (defined ('debug _ mode ')&&! DEBUG_MODE) & (defined ('cache _ control') & CACHE_CONTROL) {$ opr = $ GLOBALS ['module']. '/'. $ GLOBALS ['platform']. '/'. $ GLOBALS ['controller']. '/'. $ GLOBALS ['AC']; $ cache_name = md5 ($ opr ). TEMPLATE_SUFFIX; $ cache_file = APP_DIR. '/'. $ GLOBALS ['module']. '/'. $ GLOBALS ['platform']. '/Cache /'. $ cache_name; // clear the file state cache (in my usage, it is useless at all ..., what is the situation ??) Clearstatcache (); // The cache file does not exist. create if (! $ GLOBALS ['fopr']-> checkFile ($ cache_file) {require_once $ view_file; $ GLOBALS ['fopr']-> cFile ($ cache_file ); $ GLOBALS ['fopr']-> wData ($ cache_file, ob_get_contents ();} else {// time-out and re-cache if (time ()> filectime ($ cache_file) + CACHE_LIFE_TIME) {require_once $ view_file; // when the cache file times out, delete the original cached file $ GLOBALS ['fopr']-> dFile ($ cache_file ); // create a cache file with the same name // (when a file is created, the filectime file creation time has not been updated! How can this be solved ?? // As a result, I cannot regenerate the cache file !) $ GLOBALS ['fopr']-> cFile ($ cache_file); // write cache $ GLOBALS ['fopr']-> wData ($ cache_file, ob_get_contents (), 'W');} else {// load the cached file require_once $ cache_file;} else {// display the Dynamic File (non-cache) require_once $ view_file ;}

For your application scenarios:

  • Filemtime can better complete the Cache timeout judgment logic;

  • The file can be deleted, because the filemtime changes after the file is written again;

  • There must be a reason for deleting the file, but please refer to the "out-of-question" section in the answer 」

PHP file deletion and creation

I used the following code to perform a test:

$path = __DIR__.'/test.txt';file_put_contents($path, 'content1');echo filectime($path),PHP_EOL;unlink($path);sleep(2);file_put_contents($path, 'content2');echo filectime($path),PHP_EOL;
Result: windows (windows 10x64)

The return time is indeed the same. when I perform the second operation, the return time is still the same as the first execution time.
But I did see that this txt disappears for 2 seconds and is generated again. This logic may exist in windows (not to be further explored), which is the file property obtained after the second execution:

CentOS in linux

Returned Time,Change

Out-of-question

In the file cache engine I wrote or saw, the read cache operation, even if the time-out is detected,The Cache file is not deleted..

  • This is not an slack. after all, unlink is easy.

  • Instead, it reduces the necessary I/o requests andConcurrency brings unlink risks (Think about why ?)

  • Besides, according to inertial thinking, since you are accessing this cache, this cache will certainly be rewritten, so the reconstruction after deletion is of little significance.

  • The only problem is disk space usage. for example, some cache that has not been accessed for a long time still occupy space. in this case, we generally choose to tolerate this problem, or implement code to delete it with a scheduled task.

  • The usage of smarty to compile files is similar to that of your scenario. it does not delete files.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.