2

I have a upload form for registration picture of user by PHP.
I use Wamp Server.
I want that when the user starts the uploading file and then abandons the upload form (for any reason), that the uploaded files get deleted after 10 minutes.

how do I remove temporary files on server left from these abandoned upload forms?
OR
How do I create a temporary folder for uploaded files and empty it after a period of time?

How without using PHP code can I do this because maybe after uploading a file the user doesn't continue and the PHP script doesn't process it, so the file doesn't get deleted, but it should be deleted.

How without using PHP code can do this?
OR
How to run a PHP code without user request and by server to delete old upload files?

Martin
  • 19,815
  • 6
  • 53
  • 104
harix
  • 247
  • 2
  • 13
  • Run a clean up at boot time – RiggsFolly May 07 '17 at 18:20
  • How can I do this? – harix May 07 '17 at 18:45
  • to clarify: is this files that have been uploaded but the upload is incomplete? Or files that have been uploaded completely and then the user has not done anything with them [in the PHP code] – Martin May 07 '17 at 18:48
  • files that have been uploaded completely and then the user has not done anything with them or disconnected to continue registering or abandoned and canceled – harix May 07 '17 at 18:52
  • I have heavily rewritten your question, please let me know if my rewrite is not the question that actually want to ask? I *think* I know what you want to ask but your original was very ambiguous. Cheers – Martin May 07 '17 at 18:54
  • uploaded files are stored in a temporary folder and these are cleared out depending on the settings on your server. By their nature, they are temporary so will be deleted at some point.... – Martin May 07 '17 at 19:00
  • Thanks @Martin. Yes,your rewrite is my question. – harix May 07 '17 at 19:01

1 Answers1

1

You can set your temp_folder with http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir in the ini file or using ini function

You can get he temporary folder loaction with http://php.net/manual/en/function.sys-get-temp-dir.php


Temporary file ( ei : $_FILES['userfile']['tmp_name']) are deleted right after the script is done according to this php:: how long to tmp files stay? and the php documentation.


If you are talking about file you moved somewhere else and your server have the permission to write/delete in the folder you could do something like
foreach (glob("your_temp_folder/*") as $Filename) {

    // Read file creation time
    $FileCreationTime = filectime($Filename);

    // Calculate file age in seconds
    $FileAge = time() - $FileCreationTime; 

    // Is the file older than the given time span?
    if ($FileAge > ($expire_time * 60)){

        // Now do something with the olders files...

        print "The file $Filename is older than $expire_time minutes\n";

        // For example deleting files:
        //unlink($Filename);
    }
}

Code snippet credit to => http://www.jonasjohn.de/snippets/php/delete-temporary-files.htm

Community
  • 1
  • 1
Louis Loudog Trottier
  • 1,189
  • 12
  • 23
  • You can setup a cron job http://stackoverflow.com/questions/10097609/how-to-run-php-file-using-cron-jobs. Depending on your host and/or admin panel (cPanel, directadmin,etc) you will have different option. Many hosting compagny will provide you guide to do so. Build and test your script very well before since it will mostly be ran without any human supervision. – Louis Loudog Trottier May 07 '17 at 19:23
  • 1. Make he script to clean your temp files => 2. Test it from the command line => 3.Setup your cron job to run the script at needed interval. – Louis Loudog Trottier May 07 '17 at 19:25