I
I
Ilya Korablev2014-12-04 18:17:12
PHP
Ilya Korablev, 2014-12-04 18:17:12

Deleting files after a certain time?

Hello. There is a hosting of images (let's give an example), and it is necessary that after a certain time (well, 5 days) the files in the folder are deleted.
Well, you can have another option, if the folder is more than 100mb (for example), then the files were deleted again.
Can this be done somehow? In my best interest via .htaccess, but I will accept any other options.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander, 2014-12-04
Madzhugin @Suntechnic

What the fuck is the base? What PHP? Why is it all here? Denormalize file system data, make the solution unsupported, lame on both legs and prone to glitches?
find /path/to/directory/with/files -mtime +5 -delete
In general, man finde of course.

N
Nazar Mokrinsky, 2014-12-04
@nazarpc

It is possible, write the path to the file and the download date in the database, delete the old ones by cron. What .htaccess???

L
link_irk, 2014-12-04
@link_irk

Cron is a task scheduler for *nix systems. That is, with the help of it, tasks can be performed at certain intervals or at a certain time. A PHP script can be executed by passing its address directly to the interpreter (without using a web server).
Create a table in the database where you will enter data about uploaded files: name, upload date, size... Write a PHP script that will select records from the database that meet certain conditions (uploaded more than 5 days ago; recent records, the total size of which is 100 mb.). Then, in a loop, delete these files with the unlink function.
Now it remains to create a task for krone. For example, we will clean every day at midnight. In the server control panel, select the desired interval. As a command, specify the path to the interpreter with the parameter - the path to the script that will clean up. For example: /usr/bin/php /home/user/www/cleaner_cron.php
Look for the path to the interpreter in the hosting FAQ or ask the support service

S
Sergey Sokolov, 2014-12-04
@sergiks

It is possible without cron: when saving the next file, start cleaning at the same time. Moreover, the cleaning does not have to be complete: it is enough to erase a few old files to be deleted. After all, this process starts every time a new file is saved.
To speed up cleaning, when saving, you can name new files in such a way that they are sorted by creation time, for example, into subfolders by day and by time:

date('Ymd/His-') . substr( md5_file( $tmp_filename), 0, 4)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question