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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序