字符串到php中的压缩流 [英] String to Zipped Stream in php

查看:170
本文介绍了字符串到php中的压缩流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有服务器的处理服务器和一个服务数据库,用于以低带宽成本提供文件.在处理服务器上,php无法创建文件,因此所有内容必须先通过流完成和/或保留在内存中,然后再发送到另一台服务器进行下载.几天前,我发现了有关"php://memory"的流抽象的知识,并且我可以做类似的事情

I have a processing server with my database and a serving database to serve up files with a low bandwidth cost. On the processing server, php is not able to create files so everything must be done with streams and/or stay in memory before being sent over to another server for download. A few days ago I found out about the stream abstraction with 'php://memory' and that I can do something like

$fp=fopen('php://memory','w+');
fwrite($fp,"Hello world");
fseek($fp,0,SEEK_SET);

//make a ftp connection here with $conn_id
$upload = ftp_fput($conn_id,"targetpath/helloworld.txt",$fp,FTP_BINARY);

将文件保存在内存中,然后允许我将其ftp传送到其他服务器.这正是我想要的,除了我也想在发送数据之前对数据进行压缩-最好仅使用php的本机部分(例如ziparchive),而不使用其他自定义类进行特殊的流操作.我知道我与以下方面非常接近...

to make the file in memory and then allow me to ftp it over to my other server. This is exactly what I want, except I also want to zip the data before sending it -- preferably using only native parts of php like ziparchive and not additional custom classes for special stream manipulation. I know that I am very close with the following...

$zip = new ZipArchive();
if($zip->open('php://memory', ZIPARCHIVE::CREATE)) {
  $zip->addFromString('testtext.txt','Hello World!');
  $fp = $zip->getStream('test'); if(!$fp) print "no filepointer";

  //make a ftp connection here with $conn_id
  $upload = ftp_fput($conn_id,"targetpath/helloworld.zip",$fp,FTP_BINARY);
} else print "couldn't open a zip like that";

此操作失败的地方是对getStream的调用(尽管我认为我使用正确,但始终返回false).看起来zip可以很好地在'php://memory'中创建文件,但是出于某种原因,尽管我可能还不太了解ZipArchive如何制作zip,但是getStream仍然失败...

The point at which this fails is the call to getStream (which always returns false although I think I am using correctly). It appears that the zip is fine making the file in 'php://memory' but for some reason getStream still fails although perhaps I don't sufficiently understand how ZipArchive makes zips...

如何从字符串转到压缩的文件指针,以便可以将zip通过ftp传输到其他服务器?请记住,我无法制作任何文件,否则我只会制作zip文件,然后将其ftp覆盖.

How can I go from the string to the zipped filepointer so that I can ftp the zip over to my other server? Remember I can't make any files or else I would just make the zip file then ftp it over.

编辑:基于以下skinnynerd的建议,我尝试了以下操作

based on skinnynerd's suggestions below I tried the following

$zip = new ZipArchive();
if($zip->open('php://memory', ZIPARCHIVE::CREATE)) {
  $zip->addFromString('testtext.txt','Hello World!');
  $zip->close(); 

  $fp = fopen('php://memory','r+');
  fseek($fp,0,SEEK_SET);

  //connect to ftp
  $upload = ftp_fput($conn_id,"upload/transfer/helloworld.zip",$fp,FTP_BINARY);
}

这确实制作了一个zip并将其发送出去,但是zip的大小为0个字节,所以我认为'php://memory'的工作方式与我认为的一样……实际上在关闭步骤中失败了- $ zip-> close()返回false,这使我怀疑是否可以完全将zips打开到'php://memory'中.有人知道我可以按照这些方法尝试获得邮政编码吗?

This does make a zip and send it over but the zip is 0 bytes large so I don't think that 'php://memory' works the way I thought... it actually fails at the close step -- the $zip->close() returns false which makes me wonder if I can open zips into 'php://memory' at all. Does anyone know what I can try along these line to get the zip?

推荐答案

是否必须是Zip存档?由于您尝试保存bandwith,因此也可能是gzip.

Does it have to be a Zip archive? Since you're trying to save bandwith it could be a gzip too.

<?php
 $ftp_credentials = "ftp://USER:PASSWORD@HOST/helloworld.gz";
 $gz = gzencode("Hello World!", 9);
 $options = array('ftp' => array('overwrite' => true));
 $stream_context = stream_context_create($options);
 file_put_contents($ftp_credentials, $gz, 0, $stream_context);
?>

这篇关于字符串到php中的压缩流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆