繁体   English   中英

php - 通过 cron 运行脚本创建文件

[英]php - Creating a file by running a script via cron

我写了一个脚本来生成一些 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(); 

当我手动将 go 更改为服务器上包含脚本的文件的 URL 时,脚本将运行并创建文件。

但是,当我尝试将其作为 cron 作业运行时,脚本会运行并且 output 是通过未保存到文件而生成的。

任何想法为什么?

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

我可以想象,cronjob 只请求 header 而不是主体,因此 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;

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

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

可能需要指定完整路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM