The script generates content based on data from the database (suppose it is an upload in csv format). I would like to apply archiving to the result, but so as not to create temporary files on the disk.

Is it possible to implement this in memory and how?

While doing so:

final class ClassPrefixGzipStream extends ClassPrefixStream { private $handle; final public function open() { $this->handle = gzopen(TEMP_PATH . $this->uniqueID, "w9"); } final public function write($string) { gzwrite($this->handle, $string); } final public function close() { gzclose($this->handle); } } 

    1 answer 1

    Use the gzdeflate() method

    This function compress the given string using the DEFLATE data format.

    For details on the DEFLATE Compressed Data Format Specification version 1.3 (RFC 1951).

    • Did I understand correctly: can gzcompress be then? I have a hunch that this will not work for me. Having taken an approximate principle of work of archivers, it will be more active if the entire file is archived as a whole rather than separately. I would like to create a file in memory and then send it to the archive. For example, fopen ('php: // memory', 'r +') or fopen ('php: // temp / mamemory: 4096', 'r +') ... - org
    • one
      Yes, you can and gzcompress (). What does it mean more actively? Should compression be faster, higher compression, or lower consumption of CPU resources? Which characteristic is bothering you? --- I think the result with php: // memory will not fundamentally be different. If you already want to archive the final file, then the easiest way to use [tmpfs] [1], in many distributions is it mounted by default in / dev / shm [1]: ru.wikipedia.org/wiki/Tmpfs - Ilya Pirogov jul
    • I want to give a compressed file on the fly on the fly, without using temporary files. And if I use gzcompress, the winnings will not be the best, because I can send him portions from 5 to 40 bytes from a megabyte file. Therefore, I want to feed everything at once. - org
    • What prevents to accumulate portions in the buffer to the required volume? Or use the already ready [ob_gzhandler] [1]? [1]: php.net/ob-gzhandler - Ilya Pirogov
    • one
      You can write [buffered output handlers] [1], with its compression logic. [1]: php.net/ob-start - Ilya Pirogov