views:

51

answers:

2

Is it possible to grab the stdout output data from command line tools in php?

Example:

I want to upload a dynamically server-created mix of audio files to the client. The SOX tool lets me mix the input mp3s and send the result to stdout pipe. Could I grab this mix and instantly upload it, without the need of first saving it as a tempfile?

A: 

What i think you could do is use an in memory buffer to store the MP3 mix, and then use fsockopen to open a stream to the server you want to upload it, and then manually write the headers necessary to upload the file.

For that, you have to reproduce the same kind of request a browser will typically construct when uploading a file.

Here is a link to fsockopen and the user comments contain an example on how to perform such task: http://php.net/manual/en/function.fsockopen.php

Another way of achieving this is using curl to upload the file. I haven't used cURL for this, but here is a link to an example: http://www.akchauhan.com/how-upload-file-using-curl/

alexb
A: 

There is a function called passthru(), it will pass through binary data directly to the client's browser. Just make sure your php script put the correct header before the data is passed down, like the content-type, etc.

Darkerstar
Thank you, Darkerstar! This is exactly what I need! For anyone's reference, here's an example of outputing an resized image using Imagemagick: <?php ; $command = 'convert -resize 32x32 test.png -'; passthru($command); ?> (Please note that the header has to be added!)
Cambiata