Delete files after x amount of time

Hi guys,

Im a newb here, so bear with me! Still learning the ropes.

I want to delete all files from my server that are 6 hours old since being uploaded to the site. I believe i need a cron job? I currently have this script in its framework…

Code: [Select]
<?php

//time interval for deletion to occur…
$x = 30;

//timestamp
$current_time = time();

//the file you wish to delete
$file_name = ‘file.txt’;

//timestamp
$file_creation_time = filemtime($filename);

//extract difference
$difference = $current_time - $file_creation_time;

//if difference = $x…then delete file
if ($difference == $x) {
unlink($file_name);
}

Would that work automatically or would it need a cron to run it?

And how could i test it, that bit worries me.

Many thanks.

Well for starters, your script as it stands is set to erase files that are exactly 30 seconds old.

if ($difference == $x) {

>= , maybe?

$x should be 21600, because time is measured in seconds. (60 seconds * 60 minutes * 6 hours = 21,600)

When you cron it, you will be able to specify how often it runs.
It will need a cron to run it. (I dont trust scripts that run infinite loops, personally)

Thanks lion, and you are right, i forgot from when i was just experimenting!

<?php

//time interval for deletion to occur...
$x = 14400;  //4 hours

//timestamp
$current_time = time();

//the file you wish to delete
$file_name = 'file.txt';

//timestamp
$file_creation_time = filemtime($filename);

//extract difference
$difference = $current_time - $file_creation_time;

//if difference = $x...then delete file
if ($difference >= $x) {
unlink($file_name);
}?>

Cron task, would it look like this? Im on CentOS.

#<minute> <hour> <day> <month> <dow> <command>
/* 4 * * * /link/to/command/php/file

Thanks in advanced.

the cron should look something like

  • 4 * * * php /path/to/yourfile.php

Which will run the script every 4 hours.

It should also be pointed out that this script targets one file only.

Sorry i messed up my reply, was meant to have some works in it!

How could i get it to target every file older than 4 hours in a particular directory?

am i waiting for moderation to allow my post?

? I dont understand that.

no, i thought i was!

What it is, is that i have an Uploads directory, and inside that is lots of randomly generated folders, each with an uploaded file in each. How could i get my script to delete each file within each random folder that is older than 4 hours?

[FPHP]glob[/FPHP] the Uploads directory, to get the file names (hint: Salathe pointed out a way to check multiple folders with glob in this thread), and then [FPHP]foreach[/FPHP] of the results, do your check.

Starlion, this site wont post what i reply for some reason. Ive pasted it here

http://textbin.com/384i5

Your code is again looking only for a single file -
$files = glob(‘$destination.“/”.$name.{exe}’, GLOB_BRACE);
looks for one specific file - Uploads/<random>/<filename>.exe - which incidentally wont work, because <filename> contains the extension as well, so you’re be looking for done.exe.exe

What exactly are you trying to do with this script? Be specific.

What i want the deletion script to do: I have a basic upload site and i want to delete all files that have been uploaded every 4 hours. I want to delete the files that have been uploaded rather than the directories they get placed in. I.e

/Uploads/randomdirectory/file.exe < - - - - I want to delete file.exe rather than /randomdirectory/file.exe together.

Many thanks Sir.

So first thing is: Your upload script and your delete script are entirely seperate pages.

The deletionscript should glob Uploads//.exe (assuming you only want to delete exe’s) into an array - you called it $files in your paste there, which is fine.

now [FPHP]foreach[/FPHP] member of $files, get the filemtime, figure out your difference, and if it’s older than 4 hours, delete said member. endforeach.

(bit of pseudocode there for ya)

Hi Starlion, i’ve been trying since your last post.

Would there be any way of you doing it for me? I would be willing to pay you or support this site. It’s something that has sprung up on me today as a result of a DMCA complaint and imin no position to deal with it with my skills.

Well, prefer the ‘teach a man to fish’ approach.

Post your code, and i’ll try adjusting it.

Why would you use PHP for this ?

Just stick this in a bash script

find /path/to/your/upload/directory -type f -mmin +240 -exec rm {} \\;

(source: http://www.linuxquestions.org/questions/linux-general-1/bash-script-to-remove-files-older-than-3-days-462290/, modified to use -mmin instead of -mtime. 4 * 60 = 240)

:slight_smile:

Thanks again, really helps me.

<?php

//Script will delete files that are 4 hours old or older.

//Find all files that are .exe’s in the Uploads directory.

$files = glob(‘Uploads/.{exe}’, GLOB_BRACE);

foreach ($files as ??) {

$x = 14400;

//timestamp
$current_time = time();

//file timestamp
$file_creation_time = filemtime($files);

//extract difference
$difference = $current_time - $file_creation_time;

//if difference = $x…then delete file
if ($difference >= $x) {

unlink($filename (//do i put $files or $destination.$name here?));

}

else {

//do nothing?

}

?>

Thanks Scallio, i saw something like that around, but i had an issue.

The directories within Uploads that have the files in them, are randomly named. Would there by a way of incorporating that? I dont want to delete the directories if possible.

And how could that code above be made to run every x, just by plugging it into crontab -e ?

Scallio: The brief given says not to delete the subdirectories, that the files are inside subdirectories, and only to delete .exes.


<?php
//Script will delete files that are 4 hours old or older.

//Find all files that are .exe's in the Uploads directory's subdirs.
$files = glob('Uploads/*/*.exe');

foreach ($files as $file) {

$x = 14400;

//timestamp
$current_time = time();

//file timestamp
$file_creation_time = filemtime($file);

//extract difference
$difference = $current_time - $file_creation_time;

//if difference = $x...then delete file
if ($difference >= $x) {

unlink($file);

}//EndIf
}//EndForeach

?>

StarLion, i could kiss you. Seems to work a charm. Will try and find a way to test, maybe by setting delete time to 1 min and do it that way.

Appreciate it very much.

Thanks for your help Scallio.