Why does my Cloud Sites PHP cron job fail to include files?


Contents

Command Language: PHP vs HTTP

Some PHP scripts are designed to be run from a web browser; if this is the case for your script, you should select http as the command language. Other PHP scripts are designed to be run from PHP directly on the server; if this is the case for your script, you should select php as the command language.

Selecting the incorrect command language for your cron job can cause it to fail to execute properly or to obtain invalid results or output. However, choosing the correct command language is entirely based upon the code in the PHP script; it's therefore recommended to request assistance from the developer of the PHP script if you are unsure of the proper way it should be run.

For more information about the command language and setting up a cron job, please see How do I schedule a cron job?

Relative vs Absolute Paths

If you select php as the command language for your cron job, relative paths may not work. This is because some PHP scripts expect to be run from the directory where they are stored, and due to the nature of our Cloud Sites platform they will be run from another location. This means including or requiring files from a relative path may result in errors such as:

PHP Warning:  include(../example.php): failed to open stream: No such file or directory

A relative include looks something like this:

include('example.php');

Or this:

include('../example.php');

If you receive an error when using relative paths, this does not necessarily indicate that the file does not exist at the location you expect to find it; it just means that the PHP script is not being executed from the current directory, and so the script is looking in the wrong directory to find those files.

Solution #1

To correct this problem, you could use the absolute path to reference any files in your script. An absolute include looks something like this:

include('/mnt/stor1-wc1-dfw1/123456/www.example.com/web/content/example.php');

You will want to change the path in the example above to use your website's absolute path to the file being included. Please see this article to locate your website's absolute path.

Solution #2

Another solution is to indicate in the PHP script that you would like files to be referenced from the script's current working directory. To do this, you could use a snippet of code such as the following near the top of your script:

chdir(dirname(__FILE__));

This changes the current working directory to match the script's directory by using the __FILE__ magic constant.



Was this content helpful?




© 2011-2013 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER