Create sitemap.xml.gz with PHP - 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.

Related

Gz giving unreadable data

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.

fwrite or file_get_contents not copying full file

The file in the file path contains no JavaScript; it's just plain HTML or PHP scripts. They are also local. I'm trying to convert any file in the main path to UTF-8 before it is stored as a link in a menu. I have no control of the files that are uploaded except that they are either PHP or HTML.
An example is that the file size of one file is changed from 6,808 to 572 with the following:
$filesize = filesize($filepath);
$filecontent = file_get_contents($filepath,FALSE,NULL,0,$filesize);
$utf8content = iconv(mb_detect_encoding($filecontent, mb_detect_order(),
true), "UTF-8", $filecontent);
$fp = fopen($filepath, 'w');
fwrite($fp, $utf8content);
fclose($fp);

Save browser image into Directory

How to save the image from browser into Directory by PHP code..
First I pass an image into a php file via:
<http://localhost/filename.php?image=image.png>
I have code like below in filename.php:
$image = $_GET['image'];
header('Content-type: image/png');
imagepng($image);
now the image will be shown in the browser. but how I save it into directory?
I tried this
file_put_contents(DIR_IMAGE.'watermark.png', file_get_contents(the link http://.....=image.png));
the files is saving into the directory but the file is corrupted/damage. how can I do workable? Thanks
You can use fopen to open directory and write (save) the file. (fopen php manual)
$f = fopen('/home/www/path/image.jpg', 'w');
fwrite($f, $image);
fclose($f);

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.

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