0
我正在运行OS Ububtu 16.04。我的最终目标是能够每分钟运行一个PHP脚本作为cron作业。下面是PHP脚本位于/var/www/html/Tests/Test/index.php
:PHP脚本在目录中查找某些扩展名的文件,在浏览器中运行时返回填充数组,但从终端运行时返回空数组
<?php
$directoryPath = $_SERVER['DOCUMENT_ROOT']."/Tests/Test/data/";
echo "directoryPath: $directoryPath<br><br>";//check
$imageFilePathsArray = glob($directoryPath . "*.{png,gif}", GLOB_BRACE);
echo "<br><br>imageFilePathsArray: "; print_r($imageFilePathsArray);//check
?>
当我通过访问URL MY-IP-ADDRESS/Tests/Test/index.php
打开在浏览器这个PHP脚本,我得到:
directoryPath: /var/www/html/Tests/Test/data/
imageFilePathsArray:Array([0] => /var/www/html/Tests/Test/data/pic.png [1] => /var/www/html/Tests/Test/data/pic.gif)
书面cron作业之前,我cronTab文件,我需要检查是否可以使用终端从任何地方运行。所以我在/home/
位置开辟了Ubuntu的终端,并且在终端执行:
sudo php /var/www/html/Tests/Test/index.php
我得到了以下的输出:
directoryPath: /var/www/html/Tests/Test/data/
imageFilePathsArray: Array (
)
的问题是,为什么我得到一个空数组时相同的脚本是从命令行运行的,而从浏览器运行时是完全填充的数组?
$ _SERVER ['DOCUMENT_ROOT']'(就像大多数'$ _SERVER'-params一样)由Web服务器填充。当你从终端运行你的脚本时,Web服务器从不涉及,因此,该值没有设置。 –
@MagnusEriksson非常感谢。如果你可以把它写成答案,我可以将其标记为接受的答案。这也将有助于未来的游客。 – Sie
添加了答案。 –