0

I am creating a system that will delete a file after an amount of time that the user specifies. The issue is, that I have no idea what the format of cronjob would be to get this function. I searched Google and all the results are how to delete all files. not 1.

  • Just write a script (PHP, BASH, or whatever) that does what you want. And then configure your crontab to run it per the schedule you desire. Cron can basically run most any command-line command you would want it to run. Your script doesn't need to be in any special format to be run by cron. – Mike Brant Mar 13 '14 at 23:33
  • It has to be run at the time of the file upload though, and I will have no input to what the user uploads. Or what they choose for the time to delete after. – user3399072 Mar 13 '14 at 23:34
  • What you you mean "it has to be run at time of the file upload"? The script to delete the file doesn't need to run at this time. You just have to have a way to "mark" the file for deletion at time of upload (i.e. store files and expiry time information in a database, flat file, or some other persistence mechanism). – Mike Brant Mar 13 '14 at 23:36
  • Could you post an example please? – user3399072 Mar 13 '14 at 23:36
  • @user33990072 An example of a script to delete a file based on some persisted data? I'm afraid SO is not a coding service. We are here to help you work through problems in code YOU write. – Mike Brant Mar 13 '14 at 23:38
  • Would their be any solution that would run at the time of an upload? – user3399072 Mar 13 '14 at 23:39
  • I am not sure what you are getting at. As I stated, a common pattern in a non-daemonized service like would be typical of PHP scripts, would be to write some data about the file to be deleted at upload time, then periodically run a process to view that data, determine which files need to be deleted, and delete them. I would imagine you could do what you are wanting to do in under 30 lines of code between both the upload time code and the deletion code. This is not a complex problem, you just need to jump in and try something. – Mike Brant Mar 13 '14 at 23:42
  • Is there a specific function to do this? I am kinda new to php about a year of experience. Any help would be greatly appreciated. – user3399072 Mar 13 '14 at 23:44
  • I think I might of just solved it. Would running a php timer then running the `unlink` function work? – user3399072 Mar 13 '14 at 23:47
  • Yes, `unlink()` is the typical way to delete a file in PHP. – Mike Brant Mar 14 '14 at 17:21

1 Answers1

0

You may consider writing a service using PHP.

It could look like this :

<?php
    while (1){
       sleep(60); //check for files to delete every minutes.
        try{
            //select files with delete date < now() from db.
            //foreach of these files, trash them

        }catch($e){
            var_dump($e);
        }
    }
?>

And launch it in a terminal (on linux : php script.php from bash command line).

Loïc
  • 11,804
  • 1
  • 31
  • 49
  • This would be very unreliable. Also you are going to bump up against script execution time limits. – Mike Brant Mar 13 '14 at 23:39
  • Except there is no such script execution time limit... Plus please tell me in what it is unreliable. – Loïc Mar 13 '14 at 23:40