Jump to content

Delete files after x amount of time


andre1990

Recommended Posts

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...

 

<?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.

Link to comment
https://forums.phpfreaks.com/topic/247269-delete-files-after-x-amount-of-time/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.