Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8503139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:40:39+00:00 2026-06-11T01:40:39+00:00

I would like the output of one process created with proc_open to be piped

  • 0

I would like the output of one process created with proc_open to be piped to another one created with proc_open (in php). For example. In bash I can do:

[herbert@thdev1 ~]$ cat foo
2
3
1
[herbert@thdev1 ~]$ cat foo | sort
1
2
3
[herbert@thdev1 ~]$ 

I would like to simulate this in php using proc_open (instead of shell_exec) in order to have control over return-codes, pipes, etc. So I want something like this:

$catPipes=array();
$sortPipes=array();
$cwd = '/tmp';
$env = array();
$catProcess = proc_open("cat foo", array(
        0 => array("pipe", "r"),
        1 => array("pipe", "w")
    ), $catPipes, $cwd, $env);

$sortProcess = proc_open("sort", array(
    0 => array("pipe", "r", $catPipes[1]),
    1 => array("pipe", "w"),
    ), $sortPipes, $cwd, $env);

echo stream_get_contents($sortPipes[1]);
fclose($sortPipes[1]);
//proc_close(this) ... proc_close(that) ... etc

Would someone know how I can simulate the “|” of bash in php, i.e. connect the second descriptor of the cat-process to the first descriptor of the sort-process? Any help would be appreciated! But please do not redirect me to shell_exec, as I want to be able to check exit-codes and log errors :).

EDIT:

My needs-to-work-business-solution btw is:

while(!feof($searchPipes[1])) fwrite($lookupPipes[0], stream_get_line($searchPipes[1], 40000));

Which is basically what the OS would do, but I do not want to my own pipe-management, as I have a kernel/posix for that, and let’s be honest, it’s not 1976 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:40:41+00:00Added an answer on June 11, 2026 at 1:40 am

    Yes, you can — but i think you have to define this the way round. That you can use the STDIN of “sort” as STDOUT pipe for “cat”. Have a look at the following, which works for me:

    <?php
    
    $txt = "a\nc\ne\nb\nd\n";
    $fh  = fopen('data://text/plain;base64,' . base64_encode($txt), 'r');
    
    $sort_pipes = array();
    $sort_proc  = proc_open(
        'sort',
        array(
            array('pipe', 'r'),
            STDOUT
        ),
        $sort_pipes
    );
    
    $cat_pipes = array();
    $cat_proc  = proc_open(
        'cat',
        array(
            $fh,
            $sort_pipes[0]
        ),
        $cat_pipes
    );
    

    In the first two rows i defined a data stream from a text string that i do not have to rely on a file somewhere in the filesystem. Note, that i have a list of unsorted characters stored in the data stream (a, c, e, b, d). Running the script above should return a sorted list to STDOUT.

    Note, that you can specify resources as descriptors, too. In this case you must omit the array notation, so:

    STDOUT
    

    instead of

    array(STDOUT)
    

    etc.

    Btw.: you can even write directly to a file specified by a file name. You can find further information on the descriptor specification in the manual entry for proc_open at http://en.php.net/manual/de/function.proc-open.php

    EDIT

    The other way works too, of course: you can also write “cat” to an STDOUT pipe array('pipe', 'w') and use $cat_pipes[1] as STDIN for “sort”. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to output debug messages in my django app at different points
I would like to output in this format: e.EE_id e.FNAME e.LNAME SUPer_id s.FNAME s.LNAME
background: i am using javascript templates, which i would like to output raw, without
I have a program that generates/'rolls' two dice. I would like to output these
I am using Ruby on Rails 3.2.2 and rspec-rails-2.8.1. I would like to output
I would like to suppress output in R when I run my R script
I would like my nmake output to go to a particular directory.
Ive a HABTM relationship where the output is like below, what i would like
I would like to append the output JAR filename of a Netbeans project with
I would like to insert the output of multiple calculations into my text document:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.