通过 cmd 运行file_put_contents不会产生与浏览器相同的结果


Running file_put_contents through cmd not yielding same results as browser

我正在尝试运行一个简单的脚本,该脚本从网站获取xml文件并将其保存在文件中,下面是函数

/* update exchange rates */
public function getLatestExchangeRates(){       
   $xml = file_get_contents("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"); 
   file_put_contents("xml/exchange_rates.xml", $xml);
}
echo "Update Exchange Rates <br>'n";
$scraper->getLatestExchangeRates();

在 Chrome 中运行此脚本有效,但在 cmd 提示符下运行时使用以下语句

C:'xampp'php'php.exe -f C:'xampp'htdocs'sites'bk'update_exchange_rates.php

在控制台中给我以下错误。

warning: file_put_contents(xml/exchange_rates.xml: failed to open stream: no such file or directory in C:....

如何解决此问题?

最简单的解决方法是使用

__DIR__

全球持续构建使用路径。

$file = __DIR__ . "/xml/exchange_rates.xml"

将为您提供当前脚本运行位置的目录,这将解决通过 apache 和命令行运行的问题。