PHP:需要路径不适用于 cron 作业?

PHP: Require path does not work for cron job?(PHP:需要路径不适用于 cron 作业?)
本文介绍了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 作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)