我设置了一个Cron作业来运行php脚本。
php脚本运行良好,只是它将文件(sitemap.xml)输出到根目录,而不是执行脚本的"/public_html/mysite.com/"目录。如果我在浏览器中运行它,它运行良好。
这是我正在使用的命令:
/usr/local/bin/php -q /home/myusername/public_html/mysite.com/buildxml.php
以下是php代码的文件部分:
$xmlfile = 'sitemap.xml';
file_put_contents($xmlfile, $xmlsitemap);
我正试图找出将文件放入"/public_html/mysite.com/"目录(而不是根目录)的最佳方法。
指定完整路径
$xmlfile = __DIR__ . '/sitemap.xml';
file_put_contents($xmlfile, $xmlsitemap);
或首先更改工作目录
chdir(__DIR__);
$xmlfile = 'sitemap.xml';
file_put_contents($xmlfile, $xmlsitemap);
注意,__DIR__
表示包含当前正在执行的脚本的文件夹的路径。您可能更喜欢指定整个路径,但它们看起来是一个相同的路径。