php runs in the windows environment:
For example:
When setting the user's avatar, 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 clean the source files saved under tmp_file regularly? ? Don't know how to do it? ? Is it possible to achieve the following:
<code>$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']); </code>
If using:
<code> set_time_limit(0); ignore_user_abort(); sleep(60); unlink('tmp_file/'.$file['name']); </code>
It can indeed be cleaned regularly. However, after executing it once, you must wait 60s before executing it again! If suddenly there are a large number of pictures to be processed in a short period of time, it will be blocked and cannot be executed.
How to enable PHP to execute an additional JS script like the JS work thread to process threads that take a lot of time without causing the main thread to become stuck and not execute? ?
php runs in the windows environment:
For example:
When setting the user's avatar, 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 clean the source files saved under tmp_file regularly? ? Don't know how to do it? ? Is it possible to achieve the following:
<code>$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']); </code>
If using:
<code> set_time_limit(0); ignore_user_abort(); sleep(60); unlink('tmp_file/'.$file['name']); </code>
It can indeed be cleaned regularly. However, after executing it once, you must wait 60s before executing it again! If suddenly there are a large number of pictures to be processed in a short period of time, it will be blocked and cannot be executed.
How to enable PHP to execute an additional JS script like the JS work thread to process threads that take a lot of time without causing the main thread to become stuck and not execute? ?
Use crontab
for scheduled tasks, this is the best choice.
PHP cannot do this in a web environment. Write a shell and then cronjob
What is the reason why you want to stay for 60 seconds? If you don’t have anything to consider, just put unlink
at the end. In this scenario, I think there is no need to increase the complexity of the code.
Why not use memcache directly? It can automatically delete the timeout cache by itself
Save the upload time, and then determine the time difference between the current time and the upload time.
Delete if the time difference is greater than 60 seconds.
Obviously, the poster installed Windows and doesn’t want to install cgwin or use crontab. You can try:
``
if(random(1,10)==6){
<code> // 扫目录 清文件</code>
}
``
I am just giving an example of random conditions,
You can use it yourself: time seed...touch time of a certain file...etc.
Every user will not have to wait...
Deleting hundreds of files is no problem.
If your disk I/O is particularly high and the server load is high
then it is better to do it in crontab.
Windows scheduled tasks can also achieve the crontab effect (although it is a bit annoying that when automatic startup is turned on, it is very painful every time to change the server password)
If you must use ignore_user_abort, then set a unique flag in the database or redis