2

I'm running a Centos 5 server and want to set up a cronjob to run on every 10th of each month, but I have to following concern.

The PHP file that I need to run sits in /var/www/html/test/ called sendMails.php

If I go to the test directory eg. cd /var/www/html/test and type in ./sendMails.php my script runs perfect.

In the test directory is a folder called template and in the template folder is a file with the name index.htm. index.htm is being read in sendMails.php via the php function file_get_contents .

Now I run the script from my home directory eg. php /home/roland/sendMails.php and I get the following error file_get_contents(template/index.htm): failed to open stream: No such file or directory and don't understand why, now this will then also fail if I setup a cron.

Any advise will be appreciated

1
  • Please if down voting this question at least tell me why???? Commented Aug 25, 2009 at 9:30

4 Answers 4

0

Have you moved the template directory too?

You are better to put the files somewhere and make the script your absolute paths. E.g. /path/to/your/script.php and in the script include(/path/to/your/include/dir/index.html)

2

As already said its better to use absolute path.

I think it is not good to hardcode the absolute path in each script, at least if there is no real necessity. This is a strong portability limit.

The right way is to calculate the absolute path at runtime using dirname(__FILE__) to retrieve the dir of the current script (the script itself, not the includer of the script if it exists)

In your case you can do

<?php #Sendmail.php file_get_contents(dirname(__FILE__).'/template/index.htm') ?> 

This has to work, everywhere!

3
  • +1 was going to suggest using dirname(__FILE__) myself but you beat me too it... Commented Aug 25, 2009 at 15:04
  • eheh, tnx Jeremy Commented Aug 25, 2009 at 15:22
  • Agreed. dirname(__FILE__) is the best way to do this Commented Aug 26, 2009 at 21:24
-1

Create a shell script that changes directory and runs the php script; then call this shell script from cronjob.

1
  • Why not to create a cronjob that one per year creates that shell script? Next years doing nothing having already done the job! ;P Commented Aug 27, 2009 at 7:08
-1

You can add "chdir('/var/www/blabla');" at the top of your PHP file.

2
  • very tricky and inelegant to me Commented Aug 27, 2009 at 7:06
  • 1
    I agree, this should be avoided. Unfortunately, it's sometimes necessary when using an include tree that makes assumptions about file locations. Commented Aug 31, 2009 at 23:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.