2

I need to run a PHP script at the scheduled time daily to update some fields in database. How I can do this?

I tried with windows scheduler and it not running the script I cant figure our the error.

Is there any tutorial or steps which helps to understand the working, so as to configure.

My Bat File:

H:\wamp\bin\php\php5.5.12\php.exe H:\wamp\www\file\file.php

Test PHP Script:

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
DonOfDen
  • 3,968
  • 11
  • 62
  • 112

3 Answers3

2

You can do this with windows scheduler with command php full\link\to\php\file.php, if this not working probably link to php.exe file is not properly linked in systems PATH variable. So you can try then something like this C:\wamp\bin\php\php5.5.12\php.exe C:\wamp\www\test.php.

Also you can use at cmd command to set up schedule task, you can read more about it here

Solution:

PHP File:

<?php
$myfile = fopen("H:\\wamp\\www\\file\\newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

BAT File:

H:\wamp\bin\php\php5.5.12\php.exe H:\wamp\www\file\file.php

Double Click BAT File will Create a newfile.txt.

DonOfDen
  • 3,968
  • 11
  • 62
  • 112
Gvidas
  • 1,964
  • 2
  • 14
  • 21
  • kindly check my update in the question i have mentioned my BAT content. – DonOfDen Nov 26 '15 at 07:32
  • I don't understand, it works perfectly on my machine. Maybe there's error in your php script? – Gvidas Nov 26 '15 at 07:41
  • I have included the test PHP script which I am running. Can you tell me about your Environment Set up.? – DonOfDen Nov 26 '15 at 07:46
  • CAn you past you BAT file content.? – DonOfDen Nov 26 '15 at 07:47
  • The problem is your fopen(), because it creates newfile.txt somewhere where task scheduler starts, try to use full path `$myfile = fopen("H:\\wamp\\www\\file\\newfile.txt", "w") or die("Unable to open file!");` this should work – Gvidas Nov 26 '15 at 07:55
1

Create a .bat file with following code:

@ECHO OFF
path\to\php.exe -f "path\to\your_file.php"

Now schedule the task in Task Scheduler using the created .bat file.

Sivaprakash
  • 455
  • 1
  • 8
  • 22
0

You, also, may have to look at one of the following applications:

  1. http://cronw.sourceforge.net/   (free)
  2. http://www.z-cron.com/
  3. http://www.visualcron.com/

Or use this Google search (Windows cron)

SaidbakR
  • 13,303
  • 20
  • 101
  • 195