如何在php中正确包含cronjobs和正常执行的文件

如何在php中正确包含cronjobs和正常执行的文件

问题描述:

So I have a script that updates all currency rates for my webshop. I am trying to have it run using a cronjob but when it's executed through a cronjob I get this error:

PHP Warning:  require(.../.../core/dbcon.php): failed to open stream: No such file or directory in /var/www/domain.com/wholesale/system/labs/cur.php on line 2
PHP Fatal error:  require(): Failed opening required '.../.../core/dbcon.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/domain.com/wholesale/system/labs/cur.php on line 2

I've also tried using the full link to include it but that seems to not work when running it manually and will also not work in cronjobs.

In the php file itself this is what I use to include it:

require(".../.../core/dbcon.php");

And when I manually run it, it returns all the currencies right, and updates them in the database.

How would I correctly require the file so it can execute using a cronjob and manually as well.

所以我有一个脚本可以更新网上商店的所有货币汇率。 我想让它运行一个 cronjob但是当它通过cronjob执行时我得到这个错误: p>

  PHP警告:require(... / ... / core / dbcon.php):无法打开 stream:第2行的/var/www/domain.com/wholesale/system/labs/cur.php中没有这样的文件或目录
 
 
 
 
 
致命错误:require():无法打开所需的'... / ... / 第2行的/var/www/domain.com/wholesale/system/labs/cur.php中的core / dbcon.php'(include_path ='。:/ usr / share / php:/ usr / share / pear')  n  code>  pre> 
 
 

我也尝试使用完整链接来包含它,但是在手动运行它时似乎不起作用,也无法在cronjobs中使用。 p>

在php文件本身中,这是我用来包含它的: p>

  require(“... / ... / core / dbcon。  php“); 
  code>  pre> 
 
 

当我手动运行它时,它会正确返回所有货币,并在数据库中更新它们。 p>

何 我会正确地要求该文件,以便它可以使用cronjob并手动执行。 p> div>

Try require("./../../core/dbcon.php"); or use absolute paths.

UPDATE

Answer has been updated - see comments below for more info.