Solved

Issue running php script from task scheduler

Posted on 2013-12-26
4
575 Views
Last Modified: 2013-12-27
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\php.exe -f "E:\wamp\www\timeoff\import_data.php"

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

Open in new window


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);

?>

Open in new window

0
Comment
Question by:ats2012
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Gary Davis earned 500 total points
ID: 39740818
I suspect that when run from the scheduler, it is unable to find the .csv file so the feof() call referencing the file fails.

$fp = fopen('timeoff_data\file.csv',"r");

Make sure the startup folder is correct or fully specify the location of the file.

Gary Davis
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39740897
Line 8: Did MySQL_Select_DB() work?  Since the script does not test for this, it may have failed and you would not know.

Line 13: $fp = fopen('timeoff_data\file.csv',"r");  Did fopen() work?   The script does not test for this either.  I am almost 100% certain that it failed, since the symptomatic messages are a signature of a failed call to fopen() followed by using the return value without testing for success.

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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39740898
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Closing Comment

by:ats2012
ID: 39741719
The path was the issue. I changed it to the E:\folder\folder path and it started working. Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Keep getting 503 on Curl request 6 32
Please show me easiest post form. 3 31
get domain with php 7 20
Before I get too far.. best way to save dates data 4 16
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now