duanfangbunao36970 2015-06-25 12:19
浏览 50
已采纳

从Crontab运行包含require_once()的PHP文件

I'm trying to run a php file from a crontab. The aim of the php is to simply send an email to a user. Including the header files of my php framework. But the crontab seems to be having a problem with the paths. I've tried changing it to an absolute path.. see test cases below.

Using require_once(http://www.test.com/inc/header.php) and running php -f test.php to the command line results in:

PHP Warning:  require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/user/public/test.com/public/deploy/cron/test.php on line 3
PHP Warning:  require_once(http://www.test.com/inc/header.php): failed to open stream: no suitable wrapper could be found in /home/user/public/trybe-ing.com/public/deploy/cron/test.php on line 3
PHP Fatal error:  require_once(): Failed opening required 'http://www.test.com/inc/header.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/user/public/test.com/public/deploy/cron/test.php on line 3

Using require_once('../../inc/header.php'); and running php -f test.php to the command line results in the php file being carried out successfully.

But looking at my cron tab :

*/1 * * * * php /home/user/public/test.com/public/deploy/cron/test.php

And looking at the results in /var/mail/, it returns this error:

Message 23:
From user@server  Thu Jun 25 13:13:02 2015
X-Original-To: user
From: root@user (Cron Daemon)
To: chrismoore@ibrahimovic
Subject: Cron <user@server> php /home/user/public/test.com/public/deploy/cron/test.php
Content-Type: text/plain; charset=UTF-8
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/user>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=user>
Date: Thu, 25 Jun 2015 13:13:02 +0100 (BST)

PHP Warning:  require_once(../../inc/header.php): failed to open stream: No such file or directory in /home/user/public/trybe-ing.com/public/deploy/cron/test.php on line 3
PHP Fatal error:  require_once(): Failed opening required '../../inc/header.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/user/public/test.com/public/deploy/cron/test.php on line 3

It seems that the crontab is not able to run the php file as it would when running it from the command line. How can I get this to work?

Can you see what i'm doing wrong and offer a solution?

Thnnks

  • 写回答

2条回答 默认 最新

  • douzhenzu0247 2015-06-25 12:25
    关注

    Try including the file with it's full path by using the __DIR__ magic constant:

    require_once(__DIR__ . '/../../inc/header.php');
    

    That way, it will be required with it's full path and work from any directory without having to change to the working directory of your PHP file first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)