2013-10-04 14:24
浏览 38

将php exec存储在会话变量中

Is is possible to store an exec' output into a session variable while its running to see it's current progress?



<?php exec ("very large command to execute", $arrat, $_SESSION['output']); ?>


<php echo $_SESSION['output']); ?>

So, when i run index.php i could close the page and navigate to follow.php and follow the output of the command live everytime i refresh the page.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dskpywvki951583595
    dskpywvki951583595 2013-10-04 14:30

    No, because exec waits for the spawned process to terminate before it returns. But it should be possible to do with proc_open because that function provides the outputs of the spawned process as streams and does not wait for it to terminate. So in broard terms you could do this:

    1. Use proc_open to spawn a process and redirect its output to pipes.
    2. Use stream_select inside some kind of loop to see if there is output to read; read it with the appropriate stream functions when there is.
    3. Whenever output is read, call session_start, write it to a session variable and call session_write_close. This is the standard "session lock dance" that allows your script to update session data without holding a lock on them the whole time.
    点赞 评论
  • dongyong3223
    dongyong3223 2013-10-04 14:33

    No, You can't implement watching in this way.

    I advise you to use file to populate status from index.php and read status from file in follow.php. As alternative for file you can use Memcache

    点赞 评论
  • dongwoqin7034
    dongwoqin7034 2013-10-04 14:33

    No, exec will run to completion and only then will store the result in session.

    You should run a child process writing directly to a file and then read that file in your browser:

    $path = tempnam(sys_get_temp_dir(), 'myscript');
    $_SESSION['work_path'] = $path;
    // Release session lock
    $process = proc_open(
        'my shell command',
            0 => ['pipe', 'r'],
            1 => ['file', $path],
            2 => ['pipe', 'w'],
    if (!is_resource($process)) {
        throw new Exception('Failed to start');
    $return_value = proc_close($process);

    In your follow.php you can then just output the current output:

    echo file_get_contents($_SESSION['work_path']);
    点赞 评论