Gossamer Forum
Home : General : Internet Technologies :

PHP: Email Script: Crontab: Driving me Nuts

Quote Reply
PHP: Email Script: Crontab: Driving me Nuts
Hello all,

I am hoping someone can help me to solve a problem that I'm having with running a PHP script via crontab...the script works perfectly fine in the web browser, but when it executes via Cron, I get the following errors:

Code:
Warning: fopen(/includes/templates/emails/files/5.txt) [http://www.php.net/function.fopen]:
failed to create stream: No such file or directory in /home/www/directory/admin/admin_auto_cues_email.php on line 116

Warning: fwrite(): supplied argument is not a valid stream resource in /home/www/directory/admin/admin_auto_cues_email.php on line 117

Warning: fopen(/includes/templates/emails/files/5.txt) [http://www.php.net/function.fopen]:
failed to create stream: No such file or directory in /home/www/directory/admin/admin_auto_cues_email.php on line 132

Warning: fwrite(): supplied argument is not a valid stream resource in /home/www/directory/admin/admin_auto_cues_email.php on line 133

Warning: fopen(/includes/templates/emails/files/5.txt) [http://www.php.net/function.fopen]:
failed to create stream: No such file or directory in /home/www/directory/admin/admin_auto_cues_email.php on line 138

Warning: fwrite(): supplied argument is not a valid stream resource in /home/www/directory/admin/admin_auto_cues_email.php on line 139

Warning: fopen(/includes/templates/emails/files/5.txt) [http://www.php.net/function.fopen]:
failed to create stream: No such file or directory in /home/www/directory/admin/admin_auto_cues_email.php on line 141

Here are the corresponding codes where those errors in the script where these errors are occuring:

Code:
$my_file_name = $my_userid . ".txt";
$file = fopen ($_SERVER['DOCUMENT_ROOT'] . "/includes/templates/emails/files/" . $my_file_name,"w");
$fout = fwrite ($file, $insert_header);
clearstatcache();
while ($cue_row = mysql_fetch_array($cue_get_result)) {
$cue_typeid = $cue_row[0];
$cue_type = $cue_row[1];
$cue_text = $cue_row[2];
$cue_type_ext = $cue_row[3];
if ($debug == "on") {
print'USERID: ';echo $my_userid; print '- Cues Sent';
}
$insert_message = "
<!-- NEW RECORD -->
After reading your profile another EnneaMates member is " . $cue_text . "! <img src='http://" . $server_name . $GLOBALS['file_emoticons_relpath'] . "/" . $cue_typeid . "." . $cue_type_ext . "' alt='" . $cue_type . "'>
";
// Append Messages to File
$file = fopen ($_SERVER['DOCUMENT_ROOT'] . "/includes/templates/emails/files/" . $my_file_name,"a");
$fout = fwrite ($file, $insert_message);
clearstatcache();
}
// Build Footer of File
$insert_footer = "";
$file = fopen ($_SERVER['DOCUMENT_ROOT'] . "/includes/templates/emails/files/" . $my_file_name,"a");
$fout = fwrite ($file, $insert_top_footer);
clearstatcache();
$file = fopen ($_SERVER['DOCUMENT_ROOT'] . "/includes/templates/emails/files/" . $my_file_name,"a");
$fout .= fwrite ($file, $insert_footer);
clearstatcache();
$file = fopen ($_SERVER['DOCUMENT_ROOT'] . "/includes/templates/emails/files/" . $my_file_name,"a");
$fout .= fwrite ($file, $insert_bottom_footer);
fclose ( $file );
clearstatcache();

BTW: I've spend a number of hours searching through PHP.net, PHPBuilder, Google, etc., and can't come up a concrete solution for the above problem.

I'd like to solve this problem ASAP...since the emails need to be sending on a consistent basis...

Any suggestions are direct links to possibly solutions would be greatly appreciated.

Thanks in advance.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] PHP: Email Script: Crontab: Driving me Nuts In reply to
Does it run via SSH?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Stealth] PHP: Email Script: Crontab: Driving me Nuts In reply to
Yeah, should run via browser and SSH, but since $_SERVER['DOCUMENT_ROOT'] is undefined, the paths translate to relative paths which will fail if you run the script via cron. Entries in the _SERVER array are created by the webserver, and there is no guarantee that every webserver will provide any of these; servers may omit some, or provide others not listed here. Have you written a test PHP script to see what is output for $_SERVER['DOCUMENT_ROOT'] (to see if it is defined on your server)?

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] PHP: Email Script: Crontab: Driving me Nuts In reply to
Thanks, Dan...that makes perfect sense with what I've been reading about environmental variables and Crontab, doesn't seem to be unique to PHP, some other languages like Perl, I've seen similar errors in the past.

So, I guess what I need to do is use the full absolute path in the codes, rather than relative paths, which is what is being read by the script by Cron.

Also, makes sense since when I had changed the "absolute path" to replace the front of it with $_SERVER['DOCUMENT_ROOT'], the script did not work...so I guess that is the culprit...

I'll edit the script accordingly and let you know how it turns out....

Thanks!

EDIT: Sure wish that my WinCVS was working properly....having problems with version control, which means re-coding...
========================================
Buh Bye!

Cheers,
Me

Last edited by:

Stealth: Oct 23, 2003, 11:15 AM