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’;
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)
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.
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.
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.
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.
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 ?