Search code examples
phpforeachcroninclude

Include multiple php files in one line for linux cronjob


I have a lot of dynamic images based in php files. When I visit these files, the content (image) is updated. I use this command lines for visit the php files in the linux cronjob:

*/10 * * * * curl -q https://website.com/folder/A1-imageproduct1.php
*/10 * * * * curl -q https://website.com/folder/A1-imageproduct2.php
*/10 * * * * curl -q https://website.com/folder/A1-imageproduct3.php

I would like to use a single command line, since it is a very large number of files. I tried to create a php file with this code:

<?php
        foreach (glob("A1-*.php") as $name)

    {
        include($name);
    }
?>

but only includes the first file, the rest are apparently ignored, both visiting the file with linux cronjob and from my web browser.


Solution

  • If you have to make a call from curl you should do something like that

    <?php
     $llamar = new class{
       public function curl($file){
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, "https://website.com/folder/".$file);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_exec($ch);
         curl_close($ch);
       }
     };
     foreach (glob("A1-*.php") as $name){
       $llamar->curl($name);
     } 
    ?>
    

    If you don't recover all the files it would be better to use this class Change "RUTE" to the path of your directory

    $llamar = new class{
        public function curl($file){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, "https://website.com/folder/".$file);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_exec($ch);
          curl_close($ch);
        }
      };
    $archivo = new class{
        public function leer($carpeta)
        {
            $archivos = array();
            if (is_dir($carpeta)) {
                if ($dir = opendir($carpeta)) {
                    while (($archivo = readdir($dir)) !== false) {
                        if ($archivo != '.' && $archivo != '..' && $archivo != '.htaccess') {
                            if (file_exists($carpeta . '/' . $archivo)) {
                                if (!is_dir($carpeta . '/' . $archivo)) {
                                    $archivos[] = $archivo;
                                }
                            }
                        }
                    }
                    closedir($dir);
                }
            }
            return $archivos;
        }
    }
    
    
      $archivos = $archivo->leer("RUTE");
      for($i=0;$i<count($archivos);$i++){
        $llamar->curl($archivos[$i]);
      }