php:从 cli 工具获取 stdout 输出数据?
是否可以从 php 中的命令行工具获取 stdout 输出数据?
示例:
我想将服务器动态创建的音频文件混合上传到客户端。 SOX 工具允许我混合输入 mp3 并将结果发送到标准输出管道。 我可以抓住这个混音并立即上传它,而不需要先将其保存为临时文件吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个名为 passthru() 的函数,它将把二进制数据直接传递到客户端的浏览器。只需确保您的 php 脚本在传递数据之前放置正确的标头,例如内容类型等。
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.
我认为你可以做的是使用内存缓冲区来存储 MP3 混合,然后使用 fsockopen 打开到你想要上传的服务器的流,然后手动写入上传文件所需的标头。
为此,您必须重现浏览器在上传文件时通常构建的相同类型的请求。
以下是 fsockopen 的链接,用户评论包含有关如何执行此类任务的示例: http://php.net/manual/en/function.fsockopen.php
实现此目的的另一种方法是使用curl 上传文件。我没有为此使用 cURL,但这里有一个示例链接: http://www.akchauhan.com/how-upload-file-using-curl/
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/