ats2012
asked on
Issue running php script from task scheduler
I am trying to setup a daily import script I created in php but the script keeps spitting out errors. If works fine when run within a webpage but I am running it like this:
E:\wamp\bin\php\php5.4.3\p hp.exe -f "E:\wamp\www\timeoff\impor t_data.php "
Errors are below.
Here is the script
E:\wamp\bin\php\php5.4.3\p
Errors are below.
PHP Warning: feof() expects parameter 1 to be resource, boolean given in E:\wam
p\www\timeoff\import_data.php on line 15
PHP Stack trace:
PHP 1. {main}() E:\wamp\www\timeoff\import_data.php:0
PHP 2. feof() E:\wamp\www\timeoff\import_data.php:15
Warning: feof() expects parameter 1 to be resource, boolean given in E:\wamp\www
\timeoff\import_data.php on line 15
Call Stack:
0.0011 128712 1. {main}() E:\wamp\www\timeoff\import_data.php:0
19.1646 135816 2. feof() E:\wamp\www\timeoff\import_data.php:15
PHP Warning: fgetcsv() expects parameter 1 to be resource, boolean given in E:\
wamp\www\timeoff\import_data.php on line 16
PHP Stack trace:
PHP 1. {main}() E:\wamp\www\timeoff\import_data.php:0
PHP 2. fgetcsv() E:\wamp\www\timeoff\import_data.php:16
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in E:\wamp\
www\timeoff\import_data.php on line 16
Call Stack:
0.0011 128712 1. {main}() E:\wamp\www\timeoff\import_data.php:0
19.1664 135944 2. fgetcsv() E:\wamp\www\timeoff\import_data.php:16
Here is the script
<?php
$con = mysql_connect('host','root','pass');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ats", $con);
mysql_query('TRUNCATE TABLE timeoff_user_data') or die(mysql_error());
$fp = fopen('timeoff_data\file.csv',"r");
while( !feof($fp) ) {
if( !$line = fgetcsv($fp,10000,",", '"')) {
continue;
}
$email = str_replace("'", "", $line[0]);
$last_name = str_replace("'", "", $line[1]);
$first_name = str_replace("'", "", $line[2]);
$middle_in = str_replace("'", "", $line[3]);
$plan_id = str_replace("'", "", $line[4]);
$hours_avail = str_replace("'", "", $line[5]);
$importSQL = "INSERT INTO timeoff_user_data VALUES('".$email."','".$last_name."','".$first_name."','".$middle_in."','".$plan_id."','".$hours_avail."')";
mysql_query($importSQL) or die(mysql_error());
}
fclose($fp);
mysql_close($con);
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Kind of a sidebar note, but you probably want to get off the MySQL extension as soon as practical. PHP is doing away with MySQL support. This article explains why and what you must do to keep your scripts running in the future.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
ASKER
The path was the issue. I changed it to the E:\folder\folder path and it started working. Thanks for your help.
Line 13: $fp = fopen('timeoff_data\file.c
Testing PHP function returns for success or failure is sine qua non. Failure to test is Antipractice # 23.
You almost certainly want to examine the contents of getcwd() when the script is running as a scheduled task. It will not be the same as when the script is running as a web page. You may want to use a fully-qualified URL in fopen() or you may find a different relative path that will work for you.