Cron作业不识别PHP数组
我使用PHP和Cron作业来定期动态更新样式表(由于各种原因,JavaScript不是一种选择)。这是我跑作为cron作业脚本:Cron作业不识别PHP数组
<?php $colorstack = json_decode(file_get_contents("colors.txt"));
$color = array_shift($colorstack);
file_put_contents("color.txt",$color);
array_push($colorstack, $color);
$colors = json_encode($colorstack);
file_put_contents("colors.txt", $colors);
$iconstack = json_decode(file_get_contents("icons.txt"));
$icon = array_shift($iconstack);
file_put_contents("icon.txt",$icon);
array_push($iconstack, $icon);
$icons = json_encode($iconstack);
file_put_contents("icons.txt", $icons);
print_r ($colorstack);
print_r ($iconstack);
?>
它从两个文本文件(一组的十六进制代码,以及一组图片的文件名)返回一个字符串,并将它们放入数组。然后它抓取每个数组的第一个值,将它们写入第二组文本文件(可以通过css.php读取),然后将这些值粘贴到数组的末尾并将它们写回到文本文件中。
每次执行脚本时,它都会为样式表创建一个新的颜色十六进制代码和图像文件名,并将以前的代码发送到循环的后面。我测试过了,它在浏览器中正常工作。
问题是Cron作业不会执行脚本。相反,我不断收到以下:
警告:array_shift()预计参数1是阵列,在/path/to/file.php空给定线3
警告:array_push()预计参数1为数组,在第5行的/path/to/file.php中给出null null
依此类推。很明显,问题在于Cron作业不能解析$_____stack = json_decode(file_get_contents("_____.txt"));
作为一个数组 - 我假设它会对explode()执行相同的操作。或类似的地方代替JSON。
是否有另一种相对简洁的方式来获取这些文本文件的内容到不会遇到同一问题的数组?
回答:
这似乎是一个路径问题。 cronjob以系统进程运行,因此无法找到文件“colors.txt”和“icons.txt”。
但是,当您在浏览器中执行脚本时,它会自动从当前文件夹读取文件。
解决方案是为cronjob中的文件提供完整的系统路径。一般情况下,在cron脚本中进行任何文件读写操作时都应使用完整路径。
这里是一个示例代码:
$filePath = ''; // set it to full-path of the directory that contains the .txt files and terminate with a "/" (slash) ....
$colorstack = json_decode(file_get_contents($filePath . "colors.txt"));
....
$iconstack = json_decode(file_get_contents($filePath . "icons.txt"));
希望它能帮助!
以上是 Cron作业不识别PHP数组 的全部内容, 来源链接: utcz.com/qa/259315.html