String to Zipped Stream in php

☆樱花仙子☆ 提交于 2019-12-03 22:53:46

问题


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);

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";

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...

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.

EDIT: 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);
}

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?


回答1:


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);
?>



回答2:


$zip->getStream('test') is getting a stream to extract the file 'test' from the archive. Since there's no file 'test' in the archive, this fails. This is not the function you want to use.

As you said, what you want to do is send the finished archive to the ftp server. In this case, you would want to close the zip archive, and then reopen php://memory as a normal file (using fopen) to send it.

I don't know, but you may also be able to use $zip as a resource directly, without having to close and reopen the file.




回答3:


And I think you can try create a stream pipe directly from ftp server

<?php
    $zip = new ZipArchive();
    if($zip->open('ftp://user:password@ftp.host.com/upload/transfer/helloworld.zip', ZipArchive::CREATE)) 
    {
        $zip->addFromString('testtext.txt','Hello World!');                         
        $zip->close();
    } 
    else 
        print "couldn't open zip file on remote ftp host.";


来源:https://stackoverflow.com/questions/11488752/string-to-zipped-stream-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!