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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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