Gz giving unreadable data - php

I am creating sitemap.xml using php. In which i have to create gz file which contains number of links.
Using following code.
$gzfile = $xmlfile.".gz";
$fp = gzopen ($gzfile, 'w9');
gzwrite ($fp, $photoxmldata);
gzclose($fp);
But when i open created gz file, it gives unreadable data.
Please help.

Related

Read vbaProject.bin for macrosCode in PHP

I am trying to create a .xlsm excel file while reading the macrosCode from the vbaProject.bin file.
I implemented the following snippet:
$filename = 'private://zips/xl/vbaProject.bin';
$handle = fopen($filename, "rb");
$contents = stream_get_contents($handle);
fclose($handle);
$spreadsheet->setMacrosCode($contents);
This is a workaround because the PhpSpreadsheet library does not support generating excel file with macros. But somehow, when reading the vbaProject.bin file, the format seems incorrect, might be because of the encoding. https://i.stack.imgur.com/7SGbK.png
My question is: could I read the vbaProject.bin somehow with the proper format?

Create sitemap.xml.gz with PHP

I have a problem with compressing sitemap.xml.gz. I use this code:
// Name of the file we're compressing
$file = '/path/'.'sitemap-'.$i.'.xml';
// Name of the gz file we're creating
$gzfile = 'sitemap-'.$i.'.xml'.".gz";
// Open the gz file (w9 is the highest compression)
$fp = gzopen ($gzfile, 'w9');
// Compress the file
gzwrite ($fp, file_get_contents($file));
// Close the gz file and we're done
gzclose($fp);
When I click on link example: www.example.com/sitemap/sitemap-1.xml.gz and when I open it, I get this: IMAGE. If I wrote only www.example.com/sitemap/sitemap-1.gz I get correct file.

Unzip file with PHP

I have a gzip file. Into this file I have many csv files. I want to read only one of these files that are contained into the gzip folder. The name of the gzip folder that I want to read is 'com.instore'. And the name of the csv file is 'second'.
I use the gzopen() to read the gzip folder. But now I don't know how to read the csv file contained inside.
So, how can I do this?
Do I have to close with the gzclose()?
The gzip is into a server directory. How can I reach it?
When you gzopen you dont uncompress it. you just creates a file pointer to it.
<?php
// get contents of a gz-file into a string
$filename = "/usr/local/something.txt.gz";
$zd = gzopen($filename, "r");
$contents = gzread($zd, 10000);
gzclose($zd);
?>
while $contents is your csv file as a string.
more info # http://www.php.net/manual/en/function.gzread.php
You can extract the gzip file using this:
How can I extract or uncompress gzip file using php?
and than using this:
How to import csv file in PHP?
populate the database.

Uploading large files with PUT method

I have a page with html5 drag and drop upload feature and the file is uploading using PUT method. If I upload large image files, only part of the image is getting saved into the server. Im using the following PHP code to save the file
$putdata = fopen("php://input", "r");
$fp = fopen("/tmp/myputfile" . microtime() . ".jpg", "w");
while ($data = fread($putdata, 1024))
fwrite($fp, $data);
fclose($fp);
fclose($putdata);
Anything wrong with this ? please help
I think is becos the entire file is not completely uploaded yet when you try to read, so it sometimes will return you zero bytes read. But there might still be data being uploaded.
Maybe you can try using the feof function to check if there is any more data to be read?
see "http://www.php.net/manual/en/function.feof.php"
If you are on Windows, you should add the "b" to the mode-parameter of fopen(). see manual BTW. it is only a good idea to add the param for code-portability...

How can I unzip a .gz file with PHP?

I'm using CodeIgniter and I can't figure out how to unzip files!
PHP itself has a number of functions for dealing with gzip files.
If you want to create a new, uncompressed file, it would be something like this.
Note: This doesn't check if the target file exists first, doesn't delete the input file, or do any error checking. You really should fix those before using this in production code.
// This input should be from somewhere else, hard-coded in this example
$file_name = 'file.txt.gz';
// Raising this value may increase performance
$buffer_size = 4096; // read 4kb at a time
$out_file_name = str_replace('.gz', '', $file_name);
// Open our files (in binary mode)
$file = gzopen($file_name, 'rb');
$out_file = fopen($out_file_name, 'wb');
// Keep repeating until the end of the input file
while(!gzeof($file)) {
// Read buffer-size bytes
// Both fwrite and gzread and binary-safe
fwrite($out_file, gzread($file, $buffer_size));
}
// Files are done, close files
fclose($out_file);
gzclose($file);
Note: This deals with gzip only. It doesn't deal with tar.
gzopen is way too much work. This is more intuitive:
$zipped = file_get_contents("foo.gz");
$unzipped = gzdecode($zipped);
works on http pages when the server is spitting out gzipped data also.
If you have access to system():
system("gunzip file.sql.gz");
Use the functions implemented by the Zlib Compression extension.
This snippet shows how to use some of the functions made available from the extension:
// open file for reading
$zp = gzopen($filename, "r");
// read 3 char
echo gzread($zp, 3);
// output until end of the file and close it.
gzpassthru($zp);
gzclose($zp);
Download the Unzip library
and include or autoload the unzip library
$this->load->library('unzip');

Categories