0

XML を生成するスクリプトを作成しました。非常に基本的な方法を使用して、XML の結果をファイルに保存します。

<?php
// start the output buffer to cache the content
ob_start();

//SOME PHP CODE HERE TO GENERATE CONTENTS ON THE FILE

$cachefile = "results.xml";
// open the cache file for writing
$fp = fopen($cachefile, 'w'); 
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents()); 
// close the file
fclose($fp); 
// Send the output to the browser
ob_end_flush(); 

サーバー上のスクリプトを含むファイルの URL に手動で移動すると、スクリプトが実行され、ファイルが作成されます。

ただし、cronジョブとして実行しようとすると、スクリプトが実行され、ファイルに保存されずに出力が生成されます。

理由はありますか?

4

3 に答える 3

0

ファイルは作成されていますが、スクリプトが実行されていたディレクトリではなくルートにあります。

Run in the browser ファイルは正しいディレクトリに作成され、cron 経由で実行され、ルートに作成されます。

おそらくフルパスを指定する必要があります。

于 2012-04-07T00:13:27.707 に答える
0

おそらく許可の問題です。chmod 777 を試して、再 cron してください。

于 2012-04-07T00:08:53.750 に答える
0

cronjob はヘッダーのみを要求し、本文は要求しないため、apache は単純にヘッダーを生成せず、ob が空のままになると想像できます...次のことを試してみてください:

<?php
$out = ''

//SOME PHP CODE HERE TO GENERATE CONTENTS ON THE FILE
$out .= 'content' . "\n";
$out .= 'more' . "\n";
$out .= 'more' . "\n";
$out .= 'more' . "\n";
$out .= 'more' . "\n";

$cachefile = "results.xml";
// open the cache file for writing
$fp = fopen($cachefile, 'w'); 
// save the contents of output buffer to the file
fwrite($fp, $out); 
// close the file
fclose($fp); 
// Send the output to the browser

print$out;
于 2012-04-07T00:09:12.517 に答える