2017-07-08 109 views
1

我试图直接从php://input流中解压zip文件。我跑Laravel家园,PHP 7.1.3-3+deb.sury.org~xenial+1,与myproject.app/upload端点,这里是curl命令:解压缩或膨胀php://输入流?

curl --request POST \ 
    --url 'http://myproject.app/upload' \ 
    --data-binary "@myfile.zip" \ 

这是所有我试过的方法列表,其中所有的失败:


dd(file_get_contents('compress.zlib://php://input')); 

file_get_contents()函数:不能代表类型输入的流作为文件描述


$fh = fopen('php://input', 'rb'); 

stream_filter_append($fh, 'zlib.inflate', STREAM_FILTER_READ, array('window'=>15)); 

$data = ''; 

while (!feof($fh)) { 
    $data .= fread($fh, 8192); 
} 

dd($data); 

“”


$zip = new ZipArchive; 

$zip->open('php://input'); 
$zip->extractTo(storage_path() . '/' . 'myfile'); 
$zip->close(); 

ZipArchive :: extractTo():无效的或者未初始化的对象邮编

这里是所有我对找到的链接subject:

http://php.net/manual/en/wrappers.php#83220

http://php.net/manual/en/wrappers.php#109657

http://php.net/manual/en/wrappers.compression.php#118461

https://secure.phabricator.com/rP42566379dc3c4fd01a73067215da4a7ca18f9c17

https://arjunphp.com/how-to-unpack-a-zip-file-using-php/

我开始认为这是不可能与PHP的内置的ZIP功能流进行操作。编写临时文件的开销和复杂性会非常令人失望。有谁知道如何做到这一点,或者它是一个错误?

回答

1

经过更多的研究,我发现了答案,但并不令人满意。由于现代世界的巨大失误之一,gzip和zip格式不同。 gzip编码单个文件(这就是我们经常看到tar.gz的原因),而zip则编码文件和文件夹。我试图上传一个zip文件,并用gzip解码,但这不起作用。更多信息:

https://stackoverflow.com/a/20765054/539149

https://stackoverflow.com/a/1579506/539149

这个问题的另一部分是,PHP忽略了gzip的提供流过滤器:

https://stackoverflow.com/a/11926679/539149

因此,即使gzopen('php://temp', 'rb')作品,gzopen('php://input', 'rb')因为输入流不可回卷。这使得无法在内存流中操作,因为无法将数据写入流,然后在单独的gzip连接上读取解压缩数据。这意味着下面的代码工作:

$input = fopen("php://input", "rb"); 
$temp = fopen("php://temp", "rb+"); 
stream_copy_to_stream($input, $temp); 
rewind($temp); 
dd(stream_get_contents(gzopen('php://temp', 'rb'))); 

人们已经尝试各种解决办法,但他们都做到位摆弄:

http://php.net/manual/en/function.gzopen.php#105676

http://php.net/manual/en/function.gzdecode.php#112200

我还是设法得到一个纯粹的内存解决方案工作,但由于它不可能使用流,一个不必要的副本发生:

// works (stream + string) 
dd(gzdecode(file_get_contents('php://input'))); 

// works (stream + file) 
dd(stream_get_contents(gzopen(storage_path() . '/' . 'myfile.gz', 'rb'))); 

// works (stream + file) 
dd(file_get_contents('compress.zlib://' . storage_path() . '/' . 'myfile.gz')); 

// doesn't work (stream) 
dd(stream_get_contents(gzopen('php://input', 'rb'))); 

// doesn't work (stream + filter) 
dd(file_get_contents('compress.zlib://php://input')); 

如果没有可用的例子,我必须假设PHP的ZIP实现是不完整的,因为它不能在流操作。如果有人有更多的信息,我很乐意再次访问。请发布任何通过流实现压缩上传/下载的示例或存储库,谢谢!