简体   繁体   English

Foreach 循环在 php 的 cronjob 中不起作用

[英]Foreach loop is not working in cronjob in php

I have written a script to check only CSV files and delete them.我编写了一个脚本来仅检查 CSV 文件并删除它们。

$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:所以我尝试了一个简单的 foreach 代码,如下所示:

$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 说明 cronjob 成功运行并打印了 echo。

Email like this: Email 像这样:

./files/pvc/ ./文件/PVC/
Array大批

I don't get the echo inside the foreach loop.我没有在 foreach 循环中得到回声。

Like "file deleted" or "colors"像“文件已删除”或“颜色”

If I run the same code directly it is working fine but not via cronjob.如果我直接运行相同的代码,它可以正常工作,但不能通过 cronjob。 Any idea why?知道为什么吗?

Because csv path is relative path.因为 csv 路径是相对路径。

When cron is executed, the current directory path is the same as the php binary path(ex: /usr/bin/php).执行 cron 时,当前目录路径与 php 二进制路径相同(例如:/usr/bin/php)。

Please use the absolute path.请使用绝对路径。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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