String to Zipped Stream in php

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?

Answers


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

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


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

Need Your Help

Union and Aggregate Functions

sql sql-server-2014

I have two queries that I'm combining with a UNION operation:

Exclude files from being build in Debug

c# .net msbuild

My WP7 solution has a Class Library which contains three big embedded resources. When the XAP file is generated, it takes quite a while to compress those three files and I would like to speed up th...