如何在WAMP/Windows环境中每天自动运行PHP脚本


How can I run a PHP Script automatically Daily in WAMP / Windows Environment?

我需要在每天的预定时间运行一个PHP脚本来更新数据库中的一些字段。我该怎么做?

我尝试了windows调度程序,但它没有运行脚本,我无法计算错误。

是否有任何教程或步骤可以帮助理解工作,以便进行配置。

我的蝙蝠文件:

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

测试PHP脚本:

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

您可以使用命令php full'link'to'php'file.php的windows调度程序来执行此操作,如果此操作不起作用,则可能是指向php.exe文件的链接在系统PATH变量中没有正确链接。所以你可以试试这个C:'wamp'bin'php'php5.5.12'php.exe C:'wamp'www'test.php

你也可以使用at cmd命令来设置日程任务,你可以在这里阅读更多关于它的信息

解决方案:

PHP文件:

<?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文件:

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

双击BAT文件将创建一个新文件.txt。

使用以下代码创建.bat文件:

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

现在使用创建的.bat文件在Task Scheduler中安排任务。

您可能还需要查看以下应用程序之一:

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

或者使用此谷歌搜索(Windows cron)