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

查看:28
本文介绍了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天全站免登陆