You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中system()命令后代码无法执行,如何实现实时输出并修复?

Hey there! Let's dig into why your code after the system() call isn't running—there are a couple of key issues here that are probably causing the problem:

1. The header() call is triggering a fatal "headers already sent" error

You're echoing multiple lines of content before calling header('Location: ...'), but HTTP headers must be sent before any output (like your echo statements). When PHP tries to send a redirect header after output has already started, it throws a critical warning. If your server's error reporting is set to halt on such issues (which is standard), this will stop your script dead right after the system() call.

2. Your batch script might be terminating the parent PHP process

If your buildscript.bat uses a plain exit command instead of exit /b, it will terminate the entire command prompt process that PHP spawns to run the script. In some cases, this can take the PHP process with it, cutting off execution of your remaining code.


Here's how to fix both issues:

First, we'll use proc_open() instead of system() to run your batch script in an isolated process (so it can't take down PHP), and add output buffering to safely handle the redirect header after sending build messages.

Modified code:

function disable_ob() {
    // Turn off output buffering
    ini_set('output_buffering', 'off');
    // Turn off PHP output compression
    ini_set('zlib.output_compression', false);
    // Implicitly flush the buffer(s)
    ini_set('implicit_flush', true);
    ob_implicit_flush(true);
    // Clear, and turn off output buffering
    while (ob_get_level() > 0) {
        // Get the current level
        $level = ob_get_level();
        // End the buffering
        ob_end_clean();
        // If the current level has not changed, abort
        if (ob_get_level() == $level) break;
    }
    // Disable apache output buffering/compression
    if (function_exists('apache_setenv')) {
        apache_setenv('no-gzip', '1');
        apache_setenv('dont-vary', '1');
    }
}

function build() {
    // Start output buffering to safely send headers after content
    ob_start();
    
    $location = "build/" . date("Y-m-d-h-i-sa");
    echo "Build started! Your build location is " . $location;
    $url = "http://{$_SERVER['HTTP_HOST']}";
    $escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
    echo "
If you don´t get redirected after build go to $escaped_url/" . $location . "/smefs-Indigo-Remastered-master/Release/smef.pw.dll
";
    echo "...
";
    mkdir($location, 0777, true);
    dircpy("","buildfiles/smefs-Indigo-Remastered-master",$location . "/smefs-Indigo-Remastered-master",true);
    copy("buildfiles/buildscript.bat",$location . "/buildscript.bat");
    copy("buildfiles/JunkCode.exe",$location . "/JunkCode.exe");
    
    // Use proc_open to run the batch script in an isolated process
    $cmd = 'call ' . $location . "/buildscript.bat";
    $descriptorspec = array(
        0 => array("pipe", "r"),  // Stdin (we won't use this)
        1 => array("pipe", "w"),  // Stdout (capture batch output)
        2 => array("pipe", "w")   // Stderr (capture errors)
    );
    
    $process = proc_open($cmd, $descriptorspec, $pipes);
    
    if (is_resource($process)) {
        // Output batch script's stdout in real-time
        while (!feof($pipes[1])) {
            echo fgets($pipes[1]);
            flush(); // Ensure output shows up immediately
        }
        
        // Output any errors in red for visibility
        while (!feof($pipes[2])) {
            echo '<span style="color: red;">' . fgets($pipes[2]) . '</span>';
            flush();
        }
        
        // Clean up process and pipes
        fclose($pipes[0]);
        fclose($pipes[1]);
        fclose($pipes[2]);
        proc_close($process);
    }
    
    // These lines will now execute!
    echo "

# BUILD COMPLETED #";
    echo "If you aren´t redirected please go to this link to download build: " . $location . "/smefs-Indigo-Remastered-master/Release/smef.pw.dll";
    
    // Send redirect header after capturing all output
    header('Location: '. $location . "/smefs-Indigo-Remastered-master/Release/smef.pw.dll");
    
    // Flush all buffered content to the browser
    ob_end_flush();
}

Extra tips:

  • Open your buildscript.bat and replace any exit commands with exit /b—this exits only the batch script, not the entire command process.
  • The proc_open() method gives you more control over the batch script's output and ensures it can't crash your PHP process.

内容的提问来源于stack exchange,提问作者user5364144

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:02:15