Delete files after x amount of time

Discussion in 'PHP' started by andre199017, Sep 16, 2011.

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

    Cannot post the code here yet for some reason: but its last post here /showthread.php?t=1849721

    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.
     
    andre199017, Sep 16, 2011 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    You shurly need a cronjob. Most providers/hosters will have a control panel where you can add a cronjob. Maby contact your hosting provider?
     
    EricBruggema, Sep 17, 2011 IP
  3. jpinheiro

    jpinheiro Peon

    Messages:
    1,211
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    0
    #3
    If you are using a hosting that provides cPanel then you can easily set up cron jobs just google some tutorials on how to setup and use cron jobs
     
    jpinheiro, Sep 17, 2011 IP
  4. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    Another way that would be ignorant but fun, would be to make a looping AJAX div table with a looping PHP handler that loads, gets the time, and then has it reload,therefore restarting the script /handler .. and compares the time to the set time to erase that starts when you activate it with a button and when at 0 time left it runs a PHP script that deletes all files and folders in the name list of files/folders. last but not least erasing all text from the file of the handler itself..leaving maybe the first line on the text with // All Files Successfully Destroyed , Mr Bond.
     
    ezprint2008, Sep 17, 2011 IP
  5. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #5
    The script in post 5 would do it, but I'd change


    if ($difference == $x) {
    unlink($file_name);

    to

    if ($difference >= $x) {
    unlink($file_name);

    With the first one, if the script doesn't run within that minute, the file doesn't get deleted.

    And change

    $x = 30; to $x = 360; (6 hours).

    You definitely want a cron job. Running a looping program will eat much too much CPU time.
     
    Rukbat, Sep 17, 2011 IP