cron 作业不会打开 file_get_contents

发布于 2025-01-04 19:19:19 字数 1473 浏览 1 评论 0原文

我正在运行一个使用 file_get_contents 的 php 脚本,以便通过邮件发送包含远程文件内内容的列表。 如果我手动运行脚本,一切正常,但是当我离开它并等待 cron 运行时,它不会获取远程内容......这可能吗? 我在这里复制了一些我认为问题所在的代码:

$flyer = file_get_contents('flyer.html');

$desti = $firstname." ".$lastname;

$mail = new phpmailer();

$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "orion.xxxx.com"; // line to be changed
$mail->Port = 465; // line to be changed
$mail->Username = '[email protected]'; // line to be changed
$mail->Password = 'xxxx90'; // line to be changed
$mail->FromName = 'Bob'; // line to be changed
$mail->From = '[email protected]';// line to be changed

$mail->AddAddress($email, $desti);

$mail->Subject = 'The Gift Store';    // to be changed



if ($cover_form == '1'){ $mail->MsgHTML($long10);} else
if ($cover_form == '2'){ $mail->MsgHTML($customer);} else
if ($cover_form == '3'){ $mail->MsgHTML($freedoers);} else
if ($cover_form == '4'){ $mail->MsgHTML($freelongform);}  else
if ($cover_form == '5'){ $mail->MsgHTML($freestoreshort);}  else
if ($cover_form == '6'){ $mail->MsgHTML($getasiteshort);}  else
if ($cover_form == '7'){ $mail->MsgHTML($flyer);}  
else {}

i'm running a php script that uses file_get_contents in order to mail a list with what it's inside that remote file.
If i run the script manually everything works fine, but when i leave it and wait for the cron to run it doesn't get that remote content..... Is that possible?
i copy here a bit of the code where i think the problem is:

$flyer = file_get_contents('flyer.html');

$desti = $firstname." ".$lastname;

$mail = new phpmailer();

$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "orion.xxxx.com"; // line to be changed
$mail->Port = 465; // line to be changed
$mail->Username = '[email protected]'; // line to be changed
$mail->Password = 'xxxx90'; // line to be changed
$mail->FromName = 'Bob'; // line to be changed
$mail->From = '[email protected]';// line to be changed

$mail->AddAddress($email, $desti);

$mail->Subject = 'The Gift Store';    // to be changed



if ($cover_form == '1'){ $mail->MsgHTML($long10);} else
if ($cover_form == '2'){ $mail->MsgHTML($customer);} else
if ($cover_form == '3'){ $mail->MsgHTML($freedoers);} else
if ($cover_form == '4'){ $mail->MsgHTML($freelongform);}  else
if ($cover_form == '5'){ $mail->MsgHTML($freestoreshort);}  else
if ($cover_form == '6'){ $mail->MsgHTML($getasiteshort);}  else
if ($cover_form == '7'){ $mail->MsgHTML($flyer);}  
else {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我纯我任性 2025-01-11 19:19:19

cron 不会从您所在的“文件夹”加载代码,因此您需要指定完整路径

$flyer = file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . "flyer.html");

The cron doesn't load the code from the 'folder' you are in, so you will need to specify a full path

$flyer = file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . "flyer.html");

源来凯始玺欢你 2025-01-11 19:19:19

cron 执行时文件的路径不同

尝试

$flyer = file_get_contents(__DIR__ . '/flyer.html');

或者自己指定路径

The path of the file is different when the cron executes

Try

$flyer = file_get_contents(__DIR__ . '/flyer.html');

Or specify the path yourself

温柔戏命师 2025-01-11 19:19:19

尝试这样:

file_get_contents('flyer.html', true);

try like this:

file_get_contents('flyer.html', true);
只是在用心讲痛 2025-01-11 19:19:19

您应该确保输出和错误被重定向到一个文件,这样您就可以了解 cron 运行时脚本的情况。

crontab 中的命令如下所示:

php /path/to/your/script.php >/tmp/log.txt 2>&1

不过,看看您的代码,我建议您使用绝对路径来打开 flyer.html 文件,这样即使从包含该文件的另一个目录启动,您的脚本也能正常工作。

You should make sure that the output and errors get redirected to a file, so you can get an idea of what's going on with your script when run by cron.

The command in the crontab would look like this :

php /path/to/your/script.php >/tmp/log.txt 2>&1

Looking at your code, though, I would suggest you use an absolute path to open the flyer.html file, so your script works even if launched from another directory that the one containing that file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文