PHP:需要路径不适用于cron作业? [英] PHP: Require path does not work for cron job?

查看:176
本文介绍了PHP:需要路径不适用于cron作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cron工作,需要包括这个文件:

I have a cron job that needs to include this file:

require '../includes/common.php';

然而,当它通过cron作业运行而不是我的本地测试时,不工作。
cron作业运行以下文件(在活动服务器上):

however, when it is run via the cron job (and not my local testing), the relative path does not work. the cron job runs the following file (on the live server):

/home/username123/public_html/cron/mycronjob.php

这里是错误:

Fatal error: require(): Failed opening required '../includes/common.php' 
(include_path='.:/usr/lib/php:/usr/local/lib/php') in 
/home/username123/public_html/cron/mycronjob.php on line 2

使用与cron作业相同的绝对格式, common.php 将位于

using the same absolute format as the cron job, common.php would be located at

/home/username123/public_html/includes/common.php

这意味着我必须替换我的第2行:

does that mean i have to replace my line 2 with:

require '/home/username123/public_html/includes/common.php';

>

推荐答案

技术上看到php脚本运行在cron所在的位置;例如。如果cron在/ bin / cron中,则此语句将在/bin/includes/common.php中查找common.php。

Technically seen the php script is run where cron is located; ex. If cron was in /bin/cron, then this statement would look for common.php in /bin/includes/common.php.

必须使用完整路径或使用 set_include_path

So yeah, you'll probably have to use fullpaths or use set_include_path

set_include_path('/home/username123/public_html/includes/');
require 'common.php';

这篇关于PHP:需要路径不适用于cron作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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