addFileFromStream() public method

dds an open stream to the archive uncompressed
public addFileFromStream ( String $name, Resource $stream, array $opt = [] ) : void
$name String - path of file in archive (including directory).
$stream Resource - contents of file as a stream resource
$opt array - Hash of options for file (optional, see "File Options" below). File Options: time - Last-modified timestamp (seconds since the epoch) of this file. Defaults to the current time. comment - Comment related to this file. Examples: // create a temporary file stream and write text to it $fp = tmpfile(); fwrite($fp, 'The quick brown fox jumped over the lazy dog.'); // add a file named 'streamfile.txt' from the content of the stream $x->addFile_from_stream('streamfile.txt', $fp);
return void
 /**
  * Stream a zip file to the client
  *
  * @param String $filename  - Name for the file to be sent to the client
  * @param Array  $params    - Optional parameters
  *  {
  *    expiration: '+10 minutes'
  *  }
  */
 public function send($filename, $params = array())
 {
     // Set default values for the optional $params argument
     if (!isset($params['expiration'])) {
         $params['expiration'] = '+10 minutes';
     }
     // Initialize the ZipStream object and pass in the file name which
     //  will be what is sent in the content-disposition header.
     // This is the name of the file which will be sent to the client.
     $zip = new ZipStream\ZipStream($filename);
     // Get a list of objects from the S3 bucket. The iterator is a high
     //  level abstration that will fetch ALL of the objects without having
     //  to manually loop over responses.
     $result = $this->s3Client->getIterator('ListObjects', $this->params);
     // We loop over each object from the ListObjects call.
     foreach ($result as $file) {
         // We need to use a command to get a request for the S3 object
         //  and then we can get the presigned URL.
         $command = $this->s3Client->getCommand('GetObject', array('Bucket' => $this->params['Bucket'], 'Key' => $file['Key']));
         $signedUrl = $command->createPresignedUrl($params['expiration']);
         // Get the file name on S3 so we can save it to the zip file
         //  using the same name.
         $fileName = basename($file['Key']);
         // We want to fetch the file to a file pointer so we create it here
         //  and create a curl request and store the response into the file
         //  pointer.
         // After we've fetched the file we add the file to the zip file using
         //  the file pointer and then we close the curl request and the file
         //  pointer.
         // Closing the file pointer removes the file.
         $fp = tmpfile();
         $ch = curl_init($signedUrl);
         curl_setopt($ch, CURLOPT_TIMEOUT, 120);
         curl_setopt($ch, CURLOPT_FILE, $fp);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         curl_exec($ch);
         curl_close($ch);
         $zip->addFileFromStream($fileName, $fp);
         fclose($fp);
     }
     // Finalize the zip file.
     $zip->finish();
 }
示例#2
0
 public function testAddFileFromStream()
 {
     list($tmp, $stream) = $this->getTmpFileStream();
     $zip = new ZipStream(null, array(ZipStream::OPTION_OUTPUT_STREAM => $stream));
     $streamExample = fopen('php://temp', 'w+');
     fwrite($streamExample, "Sample String Data");
     rewind($streamExample);
     // move the pointer back to the beginning of file.
     $zip->addFileFromStream('sample.txt', $streamExample);
     fclose($streamExample);
     $streamExample2 = fopen('php://temp', 'w+');
     fwrite($streamExample2, "More Simple Sample Data");
     rewind($streamExample2);
     // move the pointer back to the beginning of file.
     $zip->addFileFromStream('test/sample.txt', $streamExample2);
     fclose($streamExample2);
     $zip->finish();
     fclose($stream);
     $tmpDir = $this->validateAndExtractZip($tmp);
     $files = $this->getRecursiveFileList($tmpDir);
     $this->assertEquals(array('sample.txt', 'test/sample.txt'), $files);
     $this->assertEquals(file_get_contents($tmpDir . '/sample.txt'), 'Sample String Data');
     $this->assertEquals(file_get_contents($tmpDir . '/test/sample.txt'), 'More Simple Sample Data');
 }