How does PHP periodically delete the cache??

Source: Internet
Author: User
PHP is running in a Windows environment:
For example:
When the user picture is set, the user uploads the source file and the server saves the source file and the modified file. The source file is stored in the Tmp_file file directory, and the modified file is placed in the Header_file directory.

I want to regularly clean up the source files saved under Tmp_file?? Do not know how to do?? Can you achieve the following:

$file=$_FILES['upload_img'];// 保存源文件move_uploaded_file($file['tmp_name'],'tmp_file/'.$file['name']);// 在对源文件做相应处理(图片裁切、缩放之类的操作)$cav=imagecreatefromjpeg('tmp_file/'.$file['name']);.......// 保存处理好的文件imagejpeg($cav,'header_file/'.$file['name']);// 定时清理掉对应源文件,这一步怎么实现??unlink('tmp_file/'.$file['name']);

If you use:

   set_time_limit(0);   ignore_user_abort();   sleep(60);   unlink('tmp_file/'.$file['name']);

It is true that it can be cleaned regularly. Once executed, however, you must wait for 60s to execute again! If you suddenly have a large number of pictures to be processed in a short time, you will not be able to do it.

How to make PHP like JS work thread, you can execute a JS script to deal with a lot of time-consuming threads without causing the main measuring modules to execute??

Reply content:

PHP is running in a Windows environment:
For example:
When the user picture is set, the user uploads the source file and the server saves the source file and the modified file. The source file is stored in the Tmp_file file directory, and the modified file is placed in the Header_file directory.

I want to regularly clean up the source files saved under Tmp_file?? Do not know how to do?? Can you achieve the following:

$file=$_FILES['upload_img'];// 保存源文件move_uploaded_file($file['tmp_name'],'tmp_file/'.$file['name']);// 在对源文件做相应处理(图片裁切、缩放之类的操作)$cav=imagecreatefromjpeg('tmp_file/'.$file['name']);.......// 保存处理好的文件imagejpeg($cav,'header_file/'.$file['name']);// 定时清理掉对应源文件,这一步怎么实现??unlink('tmp_file/'.$file['name']);

If you use:

   set_time_limit(0);   ignore_user_abort();   sleep(60);   unlink('tmp_file/'.$file['name']);

It is true that it can be cleaned regularly. Once executed, however, you must wait for 60s to execute again! If you suddenly have a large number of pictures to be processed in a short time, you will not be able to do it.

How to make PHP like JS work thread, you can execute a JS script to deal with a lot of time-consuming threads without causing the main measuring modules to execute??

For scheduled Tasks crontab , this is the best option.

PHP can't do it in a Web environment, write a shell, and then cronjob

What kind of reason makes you want to stay for 60 seconds, if nothing to consider, at the end of unlink it. In this scenario, I don't think it's necessary to increase the complexity of the code.

Why not directly with Memcache, he himself can implement the timeout cache automatically deleted

Save the upload time and then determine the time difference between the current and upload times.

The time difference is greater than 60 seconds to delete.

Obviously the landlord installed Windows do not want to install Cgwin, do not want to use crontab, you can try:

``
if (random (1,10) ==6) {

  // 扫目录 清文件

}
``

Random conditions I'm just holding a chestnut,
You can play it by yourself: Time seed ah ... time to touch a file ... Wait, wait.
Not every user has to wait ah ...

Delete Bay file no problem,
If your disk I/O is particularly high, the server load is high
Let's do it in the crontab.

    1. Windows Timing task is also can do crontab effect yo (although a bit of pit dad is, turn on auto start, every time you change the server password is very painful)

If you are not using Ignore_user_abort, then set a flag uniqueness on the database or Redis

  • 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.