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 に答える