简体   繁体   中英

Foreach loop is not working in cronjob in php

I have written a script to check only CSV files and delete them.

$dir = $getData['ftp_location']; //get the dir locations from db
echo $dir;
echo nl2br("\n"); 

$files = glob("$dir/*.csv"); //get only csv files

echo $files;
echo nl2br("\n");

foreach ($files as $tmpname)
{
   //do some stuff. I have also done echo here it is not working.
   echo "File Deleted";
}

So I tried a simple foreach code like this:

$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) 
{
      echo "$value <br>";
}

Note: On every run, I get an email stating cronjob ran successfully with the echo printed.

Email like this:

./files/pvc/
Array

I don't get the echo inside the foreach loop.

Like "file deleted" or "colors"

If I run the same code directly it is working fine but not via cronjob. Any idea why?

Because csv path is relative path.

When cron is executed, the current directory path is the same as the php binary path(ex: /usr/bin/php).

Please use the absolute path.

$dir = __DIR__. DIRECTORY_SEPARATOR. $getData['ftp_location']; //get the dir locations from db

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM