Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

migrated from superuser.com Aug 25 '09 at 9:38

This question came from our site for computer enthusiasts and power users.

    
Please if down voting this question at least tell me why???? –  Roland Aug 25 '09 at 9:30

4 Answers 4

up vote 0 down vote accepted

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)

share|improve this answer

As already said its better to use absolute path.

I think i**t 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!

share|improve this answer
    
+1 was going to suggest using dirname(__FILE__) myself but you beat me too it... –  Jeremy Bouse Aug 25 '09 at 15:04
    
eheh, tnx Jeremy –  AlberT Aug 25 '09 at 15:22
    
Agreed. dirname(__FILE__) is the best way to do this –  Brandon Aug 26 '09 at 21:24

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

share|improve this answer
    
Why not to create a cronjob that one per year creates that shell script? Next years doing nothing having already done the job! ;P –  AlberT Aug 27 '09 at 7:08

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

share|improve this answer
    
very tricky and inelegant to me –  AlberT Aug 27 '09 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. –  staticsan Aug 31 '09 at 23:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.