How do you create a .gz file using PHP?
The other answers here load the entire file into memory during compression, which will cause 'out of memory' errors on large files. The function below should be more reliable on large files as it reads and writes files in 512kb chunks.
/**
* GZIPs a file on disk (appending .gz to the name)
*
* From http://stackoverflow.com/questions/6073397/how-do-you-create-a-gz-file-using-php
* Based on function by Kioob at:
* http://www.php.net/manual/en/function.gzwrite.php#34955
*
* @param string $source Path to file that should be compressed
* @param integer $level GZIP compression level (default: 9)
* @return string New filename (with .gz appended) if success, or false if operation fails
*/
function gzCompressFile($source, $level = 9){
$dest = $source . '.gz';
$mode = 'wb' . $level;
$error = false;
if ($fp_out = gzopen($dest, $mode)) {
if ($fp_in = fopen($source,'rb')) {
while (!feof($fp_in))
gzwrite($fp_out, fread($fp_in, 1024 * 512));
fclose($fp_in);
} else {
$error = true;
}
gzclose($fp_out);
} else {
$error = true;
}
if ($error)
return false;
else
return $dest;
}
download and save a gzip file using php curl
I found a method that downloads a file to a location with cUrl. Hope it helps:
<?php
function get_file1($file, $local_path, $newfilename)
{
$err_msg = '';
echo "<br>Attempting message download for $file<br>";
$out = fopen($newfilename, 'wb');
if ($out == FALSE){
print "File not opened<br>";
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_exec($ch);
echo "<br>Error is : ".curl_error ( $ch);
curl_close($ch);
//fclose($handle);
}//end function
?>
The source is : http://www.weberdev.com/get_example.php3?ExampleID=4009
Creating zip or tar.gz archive without exec
If you want to create tar.gz and you are using PHP 5.3+, you can use PharData class:
try
{
$a = new PharData('archive.tar');
// ADD FILES TO archive.tar FILE
$a->addFile('data.xls');
$a->addFile('index.php');
// COMPRESS archive.tar FILE. COMPRESSED FILE WILL BE archive.tar.gz
$a->compress(Phar::GZ);
// NOTE THAT BOTH FILES WILL EXISTS. SO IF YOU WANT YOU CAN UNLINK archive.tar
unlink('archive.tar');
}
catch (Exception $e)
{
echo "Exception : " . $e;
}
Unpack large files with gzip in PHP
gzfile() is a convenience method that calls gzopen, gzread, and gzclose.
So, yes, you can manually do the gzopen and gzread the file in chunks.
This will uncompress the file in 4kB chunks:
function uncompress($srcName, $dstName) {
$sfp = gzopen($srcName, "rb");
$fp = fopen($dstName, "w");
while (!gzeof($sfp)) {
$string = gzread($sfp, 4096);
fwrite($fp, $string, strlen($string));
}
gzclose($sfp);
fclose($fp);
}
How to gzip folder in Laravel project
Here's a helper function for you.
if (!function_exists('gzip')) {
function gzip($filename, $disk = 'local', $delete_original = false)
{
$disk = Storage::disk($disk);
$data = $disk->get($filename);
$out_file = "$filename.gz";
$gzdata = gzencode($data, 9);
$disk->put($out_file, $gzdata);
$fp = fopen($disk->path($out_file), "w");
$result = fwrite($fp, $gzdata);
fclose($fp);
if ($result && $delete_original) {
$disk->delete($filename);
}
return $result > 0;
}
}
Tested and working for me on laravel 7.x. Just make sure you have gzip compression enabled on your machine.
Related Topics
Detect If Uploaded File Is Too Large
Simple Xml Add Namespaced Child
How to Find Timezone Id from Ip Address/Country Name in PHP
How to Get the File Path in HTML <Input Type="File"> in PHP
Admin Product Pages Custom Field Displayed in Cart and Checkout
What Is the Name for the "<<<" Operator
How to Get Attribute of Node with Namespace Using Simplexml
MySQL & PHP Parameter 1 as Resource
How to Sort a Multidemensional Array by an Inner Key
Get the Values of 2 HTML Input Tags Having the Same Name Using PHP
Accessing Arrays Whitout Quoting the Key
Upload Multiple Images with Codeigniter
What Does It Mean to Run PHP in Quiet Mode
What Does Bind_Param Accomplish
Php's Preg_Replace Regex That Matches Multiple Lines
How to Fetch Facebook Likes, Share, Comments Count from an Article