File_get_contents equivalent for gzipped files

Active3 hr before
Viewed126 times

6 Answers


Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português, Stack Overflow Public questions & answers

This is way easier using stream wrappers

load more v

What is equivalent to the file_get_contents function, which reads the entire text of a text file written with the gzwrite function?,I wrote the function I was looking for based on the comments in the manual:,I tried @Sfisioza's answer, but I had problems with it. It also reads the file twice, once and does not compress, and then compresses again. Here is the compressed version:,implement custom emoticon icons for sending and receiving (I have my own images) for iPhone chat - ios

It's easier with streamlined

load more v

compress.zlib://file.gz,compress.bzip2://file.bz2,compress.zlib:// and compress.bzip2://,zlib:// -- bzip2:// -- zip:// — Compression Streams

compress.zlib:// and compress.bzip2:// are equivalent to gzopen() and bzopen() respectively, and operate even on systems that do not support fopencookie.

ZIP extension registers zip: wrapper. As of PHP 7.2.0 and libzip 1.2.0+, support for the passwords for encrypted archives were added, allowing passwords to be supplied by stream contexts. Passwords can be set using the 'password' stream context option.

load more v

The function did not return any file. The bowser became non-responsive., Ayo August 10, 2018 at 12:12 am The function did not return any file. The bowser became non-responsive. Reply ,The file_get_contents function is often used to quickly fetch a http url or resource. Usage is very simple and appears like this,Php curl tutorial - making http requests in php

$content = file_get_contents('');
load more v

function url_get_contents($Url) {
   if (!function_exists('curl_init')) {
      die('CURL is not installed!');
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $Url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $output = curl_exec($ch);
   return json_decode($output, JSON_OBJECT_AS_ARRAY);
load more v

Imagine an application route brought us to this code. But instead of serving up a file from the local file system, we want to get it from a CDN. We may substitute file_get_contents for something more elegant (like Guzzle), but under the hood it’s much the same.,The memory usage (for this image) is around 581KB. Now, how about we try to stream this instead?,fopen and file_get_contents have their own set of default options, but these are completely customizable. To define them, we need to create a new stream context:,For the purposes of this tutorial, we’re going to measure memory usage. We’ll look at how much memory is used in “traditional” scripts. We’ll implement a couple of optimization strategies and measure those too. In the end, I want you to be able to make an educated choice.

The methods we’ll use to see how much memory is used are:

// formatBytes is taken from the documentation


function formatBytes($bytes, $precision = 2) {
   $units = array("b", "kb", "mb", "gb", "tb");

   $bytes = max($bytes, 0);
   $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
   $pow = min($pow, count($units) - 1);

   $bytes /= (1 << (10 * $pow));

   return round($bytes, $precision).
   " ".$units[$pow];
load more v

Other "equivalent-undefined" queries related to "File_get_contents equivalent for gzipped files"