PHP exec 运行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19961654/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:18:05  来源:igfitidea点击:

PHP exec to run a file

phpexec

提问by Avi

I am trying for last 3 hours to tell PHP to run a simple file. I am using wamp server for windows in local host (Windows 8)

我试图在过去的 3 个小时里告诉 PHP 运行一个简单的文件。我在本地主机 (Windows 8) 中为 windows 使用 wamp 服务器

I've tried with exec()working with:

我试过exec()与:

 echo exec('whoami');

I got response nt authority.

我得到了响应 nt 权限。

Also tested with:

还测试了:

if(function_exists('exec')) {
echo "exec is enabled";
}

So it probably works?

所以它可能有效?

I am trying to run a file called tester.php

我正在尝试运行一个名为 tester.php 的文件

When I include it, its working, when I require it its working. I need to execute it in background. When I refresh file, code is working without any error, it writes to the database normally.

当我包含它时,它可以工作,当我需要它时它可以工作。我需要在后台执行它。当我刷新文件时,代码正常工作,没有任何错误,它正常写入数据库。

When i try to exec it its not working.

当我尝试执行它时它不起作用。

I tried :

我试过 :

       exec("php http://localhost/diplomski/program/defender/tester.php");
       exec("php-cli http://localhost/diplomski/program/defender/tester.php");
       exec("http://localhost/diplomski/program/defender/tester.php");

Not working, also tried:

不工作,也试过:

        exec("php http://127.0.0.1/diplomski/program/defender/tester.php");
        exec("php-cli http://127.0.0.1/diplomski/program/defender/tester.php");
        exec("php-cli d:\wamp\www\diplomski\program\defender/tester.php")

Not working also tried:

不工作也试过:

        exec("php tester.php");
        exec("php-cli tester.php");
        exec("tester.php");

Also tried:

还试过:

         $WshShell = new COM("WScript.Shell");
         $oExec = $WshShell->Run("D:\wamp\bin\php\php5.3.13\php-win.exe -f d:\wamp \www\diplomski\program\defender/tester.php", 0, false);

Tried this, its refreshing infinitely and not working:

试过这个,它无限刷新并且不起作用:

        exec("php d:\wamp\www\diplomski\program\defender/tester.php");
        exec("php-cli d:\wamp\www\diplomski\program\defender/tester.php");
        exec("d:\wamp\www\diplomski\program\defender/tester.php");

I'm starting to pull my hair out here. First time I'm trying to use exec()and I'm not very good with it or with the commands.

我开始在这里拉我的头发。我第一次尝试使用它exec(),但我对它或命令不是很好。

回答by AbraCadaver

Give the full path to the PHP executable and the full path to the PHP script. You can save the output in $outputto see what the script produced:

提供 PHP 可执行文件的完整路径和 PHP 脚本的完整路径。您可以保存输出$output以查看脚本生成的内容:

exec("d:/path/to/php.exe d:/wamp/www/diplomski/program/defender/tester.php", $output);
print_r($output);

回答by Clickbeetle

1) What version of php? If it is older then 5.4.0 php can be in safe mode, when safe mode is enabled, you can only execute files within the safe_mode_exec_dir.

1)什么版本的php?如果它是旧的,那么 5.4.0 php 可以处于安全模式,当启用安全模式时,您只能执行 safe_mode_exec_dir 内的文件。

2)Note to this function in php.net Note:

2) php.net 中该函数的注意事项 注意:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

3) So you can try this How to make php script run another php scriptyou can try this

3) 所以你可以试试这个How to make php script run another php script你可以试试这个

 <?php
 $somearg = escapeshellarg('blah');
 exec("php file2.php $somearg > /dev/null &");

4) You can create a scheduled task How to run a PHP file in a scheduled task (Windows Task Scheduler)

4)可以创建计划任务如何在计划任务中运行PHP文件(Windows Task Scheduler)