0

我正在尝试运行一个简单的脚本,该脚本从网站获取 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:....

我该如何解决这个问题?

4

1 回答 1

1

最简单的解决方法是使用

__DIR__

全局内容来构建使用的路径。

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

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

于 2012-08-13T02:11:33.290 回答