duanla3319 2014-10-19 01:25
浏览 26

exec PHP没有传递参数

I am trying to execute a php script from within another php script. I cant seem to get it to pass the parameters properly. What am I doing wrong?

First script called

<?php

 $item = "hello";
 $item2 = "world";
 exec("php scriptToBeExecuted.php arg1=".$item." arg2=".$item2." &"); 
?>

Script to be executed

<?php
var_dump($argc);
if (isset($argv)) 
{
     parse_str(implode('&', array_slice($argv, 1)), $_GET);

     $item= $_GET['arg1'];
     $item2= $_GET['arg2'];;
}
else
{
   echo "not set";
}
?>

I do not recieve any output in a browser when I try to execute this. Which isn't the main issue because I am using this to make database calls but like I said, after hours of fussing with this, it will not execute properly or at all

  • 写回答

1条回答 默认 最新

  • duanjiao8871 2014-10-19 02:25
    关注

    You have to either run this script as a command or as a web page. If the former, there is no such thing as $_GET. If the latter, there is no $argv. Try this.

    First script called

    <?php    
     $item = "hello";
     $item2 = "world";
     exec("php scriptToBeExecuted.php $item $item2"); 
    ?>
    

    Script to be executed

    <?php
    var_dump($argc);
    if (count($argv) === 3) {
         $item= $argv[1];
         $item2= $argv[2];
    } else {
       echo "not set";
    }
    ?>
    

    See also:

    PHP passing $_GET in linux command prompt

    $_SERVER['argv'] with HTTP GET and CLI issue

    评论

报告相同问题?