title: String to Zipped Stream in php
body: <p>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</p>
  
  <pre><code>$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);
  </code></pre>
  
  <p>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...</p>
  
  <pre><code>$zip = new ZipArchive();
  if($zip-&gt;open('php://memory', ZIPARCHIVE::CREATE)) {
    $zip-&gt;addFromString('testtext.txt','Hello World!');
    $fp = $zip-&gt;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";
  </code></pre>
  
  <p>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...</p>
  
  <p>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. </p>
  
  <p><strong>EDIT:</strong> based on skinnynerd's suggestions below I tried the following</p>
  
  <pre><code>$zip = new ZipArchive();
  if($zip-&gt;open('php://memory', ZIPARCHIVE::CREATE)) {
    $zip-&gt;addFromString('testtext.txt','Hello World!');
    $zip-&gt;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);
  }
  </code></pre>
  
  <p>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?</p>