错误:无法写入数据:[Errno 32]管道损坏(proc_open)

时间:2017-11-19 03:24:33

标签: php pipe proc-open

我尝试运行一个代码(它在其他机器上工作正常),但其中一台机器无法运行!

    <?php
    $descriptorspec = array(
       0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
       1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
       2 => array("file", "tmp/error-output.txt", "a") // stderr is a file to write to
    );
    $command = "/usr/local/bin/youtube-dl --restrict-filenames -f m4a https://www.youtube.com/watch?v=youtubeid --output - | /home/youtube/public_html/bin/ffmpeg-linux-64bit -i - -b:a 128k -f mp3 /home/youtube/public_html/mp3_cache/k/filename.mp3";

    if(is_resource($process = proc_open($command, $descriptorspec, $pipes))) {

        fwrite($pipes[0], '<?php print_r($_ENV); ?>');
        fclose($pipes[0]);

        echo stream_get_contents($pipes[1]);
        fclose($pipes[1]);

        // It is important that you close any pipes before calling
        // proc_close in order to avoid a deadlock
        $return_value = proc_close($process);

        echo "command returned $return_value\n";
    }

输出“error-output.txt”文件:

[youtube] koUEv1anTb8: Downloading webpage
[youtube] koUEv1anTb8: Downloading video info webpage
[youtube] koUEv1anTb8: Extracting video information
[youtube] koUEv1anTb8: Downloading MPD manifest
[download] Destination: -

[download]   0.0% of 5.14MiB at Unknown speed ETA Unknown ETA
[download]   0.1% of 5.14MiB at Unknown speed ETA Unknown ETA

ERROR: unable to write data: [Errno 32] Broken pipe
sh: line 1: 36828 Done(1)                 /usr/local/bin/youtube-dl --    restrict-filenames -f m4a https://www.youtube.com/watch?v=youtubeid --output -
     36829 Segmentation fault      | /home/youtube/public_html/bin/ffmpeg-linux-64bit -i - -b:a 128k -f mp3 mp3_cache/k/filename.mp3

我不知道我做错了什么,有人可以指出究竟是什么问题吗?

是否有另一种方法来执行此代码而不是使用PIPE?

谢谢,

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法,在最新的Linux操作系统中,你可以在一个命令中使用exec或proc_open(etc ..)运行root和用户上传的二进制文件。

我希望这会有助于其他人。