2012-04-07 44 views
0

运行一个脚本文件我已经写了一个脚本生成一些XML,使用一个非常基本的方法对XML的结果保存到一个文件IM:PHP的 - 创建由通过cron

<?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作业运行时,脚本将运行,并且输出将通过未保存到文件而生成。

任何想法为什么?

+2

你检查过文件权限吗? – 2012-04-07 00:06:14

+0

你应该看看你的错误日志,可能会有一些有趣的错误消息。 – Stony 2012-04-07 00:07:33

+0

你的日志中是否有错误?我假设你正在cli上执行脚本,而不是通过cron请求你的服务器。你是否可能从另一个位置执行它? – csupnig 2012-04-07 00:08:41

回答

0

这可能是一个权限问题。尝试chmod 777,并重新cron。

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; 
0

正在创建文件,但在根目录中而不是在运行脚本的目录中。

在浏览器中运行的文件是在正确的目录下创建的,通过cron运行并在根目录下创建。

可能需要指定完整路径。