【问题标题】:How can I run a PHP Script automatically Daily in WAMP / Windows Environment?如何在 WAMP / Windows 环境中每天自动运行 PHP 脚本?
【发布时间】:2016-02-29 04:39:15
【问题描述】:

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

我尝试使用 Windows 调度程序,但它没有运行我无法确定错误的脚本。

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

我的蝙蝠档案:

H:\wamp\bin\php\php5.5.12\php.exe H:\wamp\www\file\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);
?>

【问题讨论】:

  • 使用 cron 作业。 cron job
  • 请查看链接

标签: php cron wamp scheduled-tasks


【解决方案1】:

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

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

或使用this Google search(Windows cron)

【讨论】:

    【解决方案2】:

    您可以使用带有命令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 命令来设置调度任务,你可以阅读更多关于它here

    解决方案:

    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\file.php

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

    【讨论】:

    • 请在我提到我的 BAT 内容的问题中查看我的更新。
    • 我不明白,它在我的机器上完美运行。也许你的 php 脚本有错误?
    • 我已经包含了我正在运行的测试 PHP 脚本。你能告诉我你的环境设置吗?
    • 你能过去你的BAT文件内容吗?
    • 问题是你的 fopen(),因为它在任务调度程序开始的地方创建 newfile.txt,尝试使用完整路径 $myfile = fopen("H:\\wamp\\www\\file\\newfile.txt", "w") or die("Unable to open file!"); 这应该可以工作
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-10-30
      • 2023-03-16
      • 1970-01-01
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2017-09-25
      相关资源
      最近更新 更多