如何在计划任务中运行 PHP 文件(Windows 任务计划程序)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/295386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a PHP file in a scheduled task (Windows Task Scheduler)
提问by praveenjayapal
How can I create a scheduled task to run a PHP file?
Yes, I filled out everything in the scheduled task, but it still doesn't work.
如何创建计划任务来运行 PHP 文件?
是的,我把计划任务里的东西都填好了,但是还是不行。
Run: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website\save.php"
Start in: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website"
运行:“C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website\save.php”
开始于:“C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website”
It just opens the PHP file in Notepad.
它只是在记事本中打开 PHP 文件。
I gave the correct user name and pwd.
我给出了正确的用户名和密码。
Please help me..
请帮我..
回答by Tomalak
The Run command should be
运行命令应该是
C:\Path\to\php.exe -f "C:\Path\to\file.php"
From the command line help of php.exe:
从 php.exe 的命令行帮助:
-f Parse and execute <file>.
回答by matobago
I just wrote a .batfile that does the work file.bat
我刚刚写了一个.bat文件来完成工作file.bat
@ECHO OFF
php.exe -f "C:\code\cust.php"
And put it in the Schedule Tasks like this:
并将其放入计划任务中,如下所示:
Run: C:\code\file.bat
跑: C:\code\file.bat
Start in: C:\code\
开始于: C:\code\
回答by Martin
If you running php scripts, in most of cases script awaiting to be runned in current folder. That mean you must set up folder each your action, use field "Start In".
如果您运行 php 脚本,在大多数情况下,脚本等待在当前文件夹中运行。这意味着您必须为每个操作设置文件夹,使用字段“开始于”。
Example:
例子:
Run: C:\php\php.exe
Arguments: -f C:\web\myscript.php
Do not forget:
不要忘记:
Start in: C:\web\
回答by Jonathan Lonowski
Sounds like you don't have PHP files associated with the EXE.
听起来您没有与 EXE 关联的 PHP 文件。
You can do this My Computer> Tools> Folder Options> File Types. If nothing else, this can also help you verify your settings for them.
您可以执行此操作My Computer> Tools> Folder Options> File Types。如果不出意外,这也可以帮助您验证它们的设置。
Otherwise, you can specify "C:\path\to\php.exe [file]"in the task.
否则,您可以在任务中指定“C:\path\to\php.exe [file]”。
回答by Logan Wayne
For those people that can't able to make @Tomalak's answer work:
对于那些无法使@Tomalak 的答案起作用的人:
Check if there are spaces () in your directory. If it does, you need to enclose them with ".
检查您的目录中是否有空格 ( )。如果是这样,您需要将它们用".
"C:\Path\to php file\php.exe" -f "C:\Path\to php file\file.php"
And if it still did not work, check your file.phpif there are included files in it.
如果它仍然不起作用,请检查file.php其中是否包含包含文件。
include("file2.php"); /* OR */
include_once("file2.php");
Remove it. And if the included file in your file.phpis really needed, try to move the code/script from that included file to your main file.
去掉它。如果file.php确实需要包含的文件,请尝试将代码/脚本从包含的文件移动到主文件。
回答by charles
This is what I did:
这就是我所做的:
PHP file
<?php my code goes here ?>*Note if you are using HTTP API/CURL in CLI use
dl("php_curl.dll");this loads curl into cli
Now I added PHP to windows path variable, this can be done from My computer, properties, advanced settings, environment variables, new
Next I created a .bat file, simply open a notepad & type code below and save as myfile.bat
@ECHO OFF php -f d:\wamp\www\V3\task.phpThis sitemight help you on .bat file syntax.
Now create a new scheduled task on windows & call the above .bat file as source,
PHP文件
<?php my code goes here ?>*请注意,如果您在 CLI 中使用 HTTP API/CURL
dl("php_curl.dll");这将卷曲加载到 cli
现在我将 PHP 添加到 windows 路径变量,这可以从我的电脑、属性、高级设置、环境变量、新
接下来我创建了一个 .bat 文件,只需打开一个记事本并在下面输入代码并保存为 myfile.bat
@ECHO OFF php -f d:\wamp\www\V3\task.php该站点可能会帮助您了解 .bat 文件语法。
现在在 Windows 上创建一个新的计划任务并调用上面的 .bat 文件作为源,
回答by Fuangwith S.
I think, you must execute your PHP script via URL. you can write batch script for execute URL. Why you don't write backend script in other language such as batch script, vbscript or etc.
我认为,您必须通过 URL 执行您的 PHP 脚本。您可以为执行 URL 编写批处理脚本。为什么不使用其他语言(例如批处理脚本、vbscript 等)编写后端脚本?

