doulutian4843 2014-07-21 02:50
浏览 51
已采纳

如何从浏览器在命令行中执行外部PHP脚本

I have an external php script named external.php which includes this:

<?php
echo 'External Output';
?>

When I run that on command line with command:

php external.php

I get output as

External Output

But when the same script I execute from my browser's php file named index.php which has this code:

<?php
$exe=exec('php external.php',$out,$ret);
print_r($out);
?>

Then I get no output.

When I modify it as:

<?php
$exe=exec('php external.php 2>&1',$out,$ret);
print_r($out);
?>

Then I get this output:

php: /opt/lampp/lib/libxml2.so.2: version `LIBXML2_2.9.0' not found (required by php)

My question is, how can I execute that "external.php" file in commandline from index.php and get output on my browser (i.e. on index.php) ? I have tried system() function too, it doesn't work as well.

Edit: I cannot include the external.php in index.php because external.php can take lot of time to execute (more than 10 hours).

Edit#2 Solved it, giving the full path to PHP solved the problem.

Here is an example:

<?php
$exe=exec('full/path/to/php /full/path/to/external.php',$out,$ret);
print_r($out);
?>

Thankyou Oleg and Scopey for giving me some hint help.

  • 写回答

2条回答 默认 最新

  • douyan1613 2014-07-21 03:03
    关注

    Specify the full path of external.php:

    <?php
    $exe=exec('php /full/path/to/external.php',$out,$ret);
    print_r($out);
    ?>
    

    Try also specifying the full path to php on the command line and check that it works:

    $/usr/bin/php "/full/path/to/external.php"
    

    If that works, try it also in your php file:

    <?php
    $exe=exec('/usr/bin/php /full/path/to/external.php',$out,$ret);
    print_r($out);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器