从Crontab运行包含require_once()的PHP文件, [英] Running a PHP file from Crontab that includes require_once()

查看:416
本文介绍了从Crontab运行包含require_once()的PHP文件,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从crontab运行php文件。 php的目的是简单地向用户发送电子邮件。包括我的php框架的头文件。但是crontab似乎有路径的问题。我已尝试将其更改为绝对路径。请参阅下面的测试用例。



使用 require_once /inc/header.php)并运行 php -f test.php 到命令行,结果:



PHP警告:require_once():http://在/home/user/public/test.com/中的服务器配置中,allow_url_include = 0禁用了wrapper public / deploy / cron / test.php on line 3
PHP警告:require_once(http://www.test.com/inc/header.php):无法打开流:没有找到合适的包装器/home/user/public/trybe-ing.com/public/deploy/cron/test.php on line 3
PHP致命错误:require_once():无法打开必须'http://www.test.com /inc/header.php'(include_path ='。:/ usr / share / php:/ usr / share / pear')在/home/user/public/test.com/public/deploy/cron/test.php第3行

使用 require_once('../../ inc / header .php'); 并且在命令行中运行 php -f test.php 会导致php文件成功执行。



但是看看我的cron标签:

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

查看/ var / mail /中的结果,会返回以下错误:

 消息23:
从用户@服务器Thu Jun 25 13:13:02 2015
X-Original-To:user
从:root @ user(Cron守护程序)
到:chrismoore @ ibrahimovic
主题: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>
日期:Thu,25 Jun 2015 13:13:02 +0100(BST)

PHP警告:require_once(../../ inc / header.php):无法打开stream:/home/user/public/trybe-ing.com/public/deploy/cron/test.php中没有这样的文件或目录第3行
PHP致命错误:require_once():无法打开必需'。 ./../inc/header.php'(include_path ='。:/ usr / share / php:/ usr / share / pear')在/home/user/public/test.com/public/deploy/cron/ test.php on line 3

似乎crontab不能运行php文件将从命令行运行它。



您能看到我做错了,并提供解决方案吗?



Thnnks

解决方案

尝试使用 __ DIR __ 魔术常量

  require_once(__ DIR__。'/../../inc/header.php'); 

这样,它将需要它的完整路径和工作从任何目录,您的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

解决方案

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.

这篇关于从Crontab运行包含require_once()的PHP文件,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆