I have a large number of visitors per day and create up to 50 thousand sessions per day. I am writing a script so that when the number of sessions in the mod-tmp folder exceeds 20 thousand, the server would delete all files from it and reboot. Help, please, using PHP to remove all files from 1 folder?

  • It seems like you can not delete everything at once. You can get the list and delete in a loop, but that is a long time. You can try using exec to run an external program. Or even write a demon that will work higher than pkhp and faster - lnart
  • Quite interesting, I would say not a question but a situation. Dig to the side of cron php - Palmervan
  • ten
    > so that when the number of sessions in the mod-tmp folder increased 20 thousand, the server deleted all files from it and rebooted. Sorry, but this is a shaped nightmare, I just don’t find other words. First of all, PHP cleans up rotten sessions itself, if session.gc-* settings haven't changed. - Ilya Pirogov

7 answers 7

Inventing a bulky bike. Here is the fastest and easiest way:

 function clear() { if (file_exists('/cache/')) { foreach (glob('/cache/*') as $file) { unlink($file); } } } 

And that's all. Then call where and when needed. For example, if it is Wordpress, you hang up add_action('save_post','clear') .

    Rough approach, very rude:

     $baseDir = "./"; exec("rm -f " .$baseDir . "/dir/*"); 

    This will only work in Linux. Once again, the approach is very rough and it is better not to use this EVER.

      Probably, everything is much simpler. ISPManager crookedly changes the php config by disabling the session garbage collector.

      Open php config. By default /etc/php.d/apache/php.ini

      Change session.gc_probability=0 to 1

      Restart appetch /etc/init.d/apache2 restart

      Everything, garbage collection again - php business.

        Delete php means all at once will not work. Only in the loop. Perhaps you should look towards the demons, as @Inart said.
        Code for pkhp:

         function cleanDir($dir) { $files = glob($dir."/*"); $c = count($files); if (count($files) > 0) { foreach ($files as $file) { if (file_exists($file)) { unlink($file); } } } } 
           array_map('unlink', glob('./cache/*')); 

            And try not to remove it all at once, but gradually. That is, the PHP script of each user who came to your site will delete, for example, 1000 files each, until they remain at all. This is not difficult to do. One XML file is enough (the database is not necessary to use here) with two entries - how much is left and the second - whether it is necessary to delete.

              I have deleted everything with this script ... cleaned up ... along with the site.

               if ($ objs = glob ($ dir. "/ *")) {
                   echo $ objs;
                   foreach ($ objs as $ obj) {
                       if (is_dir ($ obj)) {
              
                       } else {
                           unlink ($ obj);
                       }
                   }
               }
               rmdir ($ dir);
              
              • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky

              Protected by spirit community member 3 May '16 at 8:43 .

              Thank you for your interest in this issue. Since he collected a large number of low-quality and spam responses, which had to be deleted, now it’s necessary to have 10 reputation points on the site (the bonus for account association is not counted ).

              Maybe you want to answer one of the unanswered questions ?