Forums

Can i delete old images of the server?

Fredolino 10 Oct, 2019
Hi,

if I use in the "upload files" the file name provider "{var: upload12.file.name}. {var: upload12.file.extension}" and upload a new image with the same name on the server, then the image correctly overwritten in the folder.

But that a user can not overwrite the image on the server by mistake or maybe for example, if multiple users upload an image with the same name at the same time, the file should be given a different name (for example, the unique "uuid" as the name).
Everything is ok so far. But there is a problem:


If then the files get a different name, then the "old" files are not overwritten anymore and to many "old" files are accumulating on the server.

For example, can I use CF6 to upload / save an image on the server to execute a SQL transaction that deletes / overwrites or adds an old file?

Or what easy way can I do with CF6 to avoid old, unused images on the server?


F.
GreyHead 10 Oct, 2019
Hi Fredolino,

If you can identify which files are no longer needed e.g. more than 6 months old - then you can run a cron job on the server every week to delete them.

Bob
healyhatman 11 Oct, 2019
Cron job:
php -q /home/srvr/public_html/yoursite.com.au/pathtoimagesfolder/old_cleanup.php >/dev/null 2>&1
old_cleanup.php with BONUS pushbullet notification:
<?php


$msg = '';

$filecount = 0;

foreach(glob("*.{[pP][nN][gG],[jJ][pP][gG],[jJ][pP][eE][gG]}", GLOB_BRACE) as $i => $file) {
if(filemtime($file) < (time() - 5*24*3600))
{
$filecount++;
unlink($file);
}
}

if(!$filecount)
$msg .= "No files to delete.";
else
$msg .= "\nDeleted $filecount files.";

pushbullet($msg);

function pushbullet($msg){

$data = json_encode(array(
'type' => 'note',
'title' => 'Images cleanup',
'body' => $msg,
// 'device_iden' => YOUR_DEVICE_ID (Only if you want to send it to one device only)
));

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.pushbullet.com/v2/pushes');
curl_setopt($curl, CURLOPT_USERPWD, 'pushbulletapikeygoeshere');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Content-Length: ' . strlen($data)]);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_exec($curl);
curl_close($curl);

}


?>

Obviously your server path will be different.
Fredolino 11 Oct, 2019
oh, :-)
the idea may be viable.

But how is it checked if the image file is still associated with a record or not?
If the file is not associated with a record, it can be deleted.
healyhatman 11 Oct, 2019
Well can't help you with that. You could always point the cron job at a chronoform with &tvout=view on the end of the URL, with read data actions and whatever else you need done.
Fredolino 14 Oct, 2019
Answer
:-)
Yes its work with cronjob and CF6

Thanx!
This topic is locked and no more replies can be posted.