PHP: Uncompressing gzcompressed file with gzuncompress and fopen - php

I am trying to uncompress a binary file and then uncompress it to test whether the compresssion is working.
However the 'uncompressed' file has the same data as the 'compressed' file. As though the uncompression never happened. I have listed code below.
thanks in advance:
//compressing
//read file
$filename = 'tocompress/tocompress'.$number_input.'.bin';
$contents=fread($fp,filesize($filename));
fclose($fp);
//compress file
$compressing = gzcompress($contents , '0');
//write to file
$fp = fopen('compressed/compressed'.$number_input.'.bin', 'wb');
fwrite($fp, $compressing);
fclose($fp);
//uncompressing
//read file
$uncompfilename='compressed/compressed'.$number_input.'.bin';
$fp=fopen($uncompfilename,'rb');
$uncompresscontents=fread($fp,filesize($uncompfilename));
fclose($fp);
//uncompress file
$uncompressing = gzuncompress($uncompresscontents);
//write to file
$fp = fopen('uncompressed/uncompressed'.$number_input.'.bin', 'wb');
fwrite($fp, $uncompresscontents);
fclose($fp);

gzcompress takes an optional second argument which sets compression level, from 0-9. You're setting it to 0 (no compression) and you should be using an int, not a string:
You have gzcompress($contents, '0');
You want gzcompress($contents, 9);
From php.net/gzcompress:
The level of compression. Can be given as 0 for no compression up to 9
for maximum compression.

Related

Is there an option to readfile() but only a range of bytes of a binary file? [duplicate]

How can I have php return just some bytes of a file? Like, it I wanted to load byte 7 through 15 into a string, without reading any other part of the file? Its important that I don't need to load all of the file into memory, as the file could be quite large.
Could use file_get_contents() using the offset and maxlen parameters.
$data = file_get_contents('somefile.txt', false, NULL, 6, 8);
Use fseek() and fread()
$fp = fopen('somefile.txt', 'r');
// move to the 7th byte
fseek($fp, 7);
$data = fread($fp, 8); // read 8 bytes from byte 7
fclose($fp);
Using Pear:
<?php
require_once 'File.php';
//read and output first 15 bytes of file myFile
echo File::read("/path/to/myFile", 15);
?>
Or:
<?php
// get contents of a file into a string
$filename = "/path/to/myFile";
$handle = fopen($filename, "r");
$contents = fread($handle, 15);
fclose($handle);
?>
Either method you can use byte 7-15 to do what you want. I don't think you can go after certain bytes without starting from the beginning of the file.

Write and read from the same file- PHP

I am trying to write to a file and then read the data from the same file. But sometimes I am facing this issue that the file reading process is getting started even before the file writing gets finished. How can I solve this issue ? How can i make file writing process finish before moving ahead?
// writing to file
$string= <12 kb of specific data which i need>;
$filename.="/ttc/";
$filename.="datasave.html";
if($fp = fopen($filename, 'w'))
{
fwrite($fp, $string);
fclose($fp);
}
// writing to the file
$handle = fopen($filename, "r") ;
$datatnc = fread($handle, filesize($filename));
$datatnc = addslashes($datatnc);
fclose($handle);
The reason it does not work is because when you are done writing a string to the file the file pointer points to the end of the file so later when you try to read the same file with the same file pointer there is nothing more to read. All you have to do is rewind the pointer to the beginning of the file. Here is an example:
<?php
$fileName = 'test_file';
$savePath = "tmp/tests/" . $fileName;
//create file pointer handle
$fp = fopen($savePath, 'r+');
fwrite($fp, "Writing and Reading with same fopen handle!");
//Now rewind file pointer to start reading
rewind($fp);
//this will output "Writing and Reading with same fopen handle!"
echo fread($fp, filesize($savePath));
fclose($fp);
?>
Here is more info on the rewind() method http://php.net/manual/en/function.rewind.php
I have mentioned the URL through which i got the solution. I implemented the same. If you want me to copy the text from that link then here it is :
$file = fopen("test.txt","w+");
// exclusive lock
if (flock($file,LOCK_EX))
{
fwrite($file,"Write something");
// release lock
flock($file,LOCK_UN);
}
else
{
echo "Error locking file!";
}
fclose($file);
Use fclose after writing to close the file pointer and then fopen again to open it.

How to handle simultaneous reading and writing

I have a server that grabs mp3 audio buffers from a source and writes it into a file via php. It truncates the beginning of the file so the file size never exceeds 2 MB. At the same time a client is streaming the mp3 by seeking to the end and reading if there is any new data. The problem is when the file get's truncated the position the client was reading at changes.
This is the client side that streams the audio:
$handle = fopen('cool.mp3', "r");
$err = fseek($handle, 0, SEEK_END);
while(file_exists($file_lock)){ // cool.mp3.lock means stream is still going
$data = fread($handle, 1024);
echo $data;
ob_flush();
flush();
}
I use this on the server to write data as I get it:
$data = "audio frames....";
clearstatcache();
$file = 'cool.mp3';
if(filesize($file) > 1024*200){ //2 MB
ftruncatestart($file, 1024*25); //Trim Down By Deleting Front
}
file_put_contents($file, $data, FILE_APPEND);

Get part of a file by byte number in php

How can I have php return just some bytes of a file? Like, it I wanted to load byte 7 through 15 into a string, without reading any other part of the file? Its important that I don't need to load all of the file into memory, as the file could be quite large.
Could use file_get_contents() using the offset and maxlen parameters.
$data = file_get_contents('somefile.txt', false, NULL, 6, 8);
Use fseek() and fread()
$fp = fopen('somefile.txt', 'r');
// move to the 7th byte
fseek($fp, 7);
$data = fread($fp, 8); // read 8 bytes from byte 7
fclose($fp);
Using Pear:
<?php
require_once 'File.php';
//read and output first 15 bytes of file myFile
echo File::read("/path/to/myFile", 15);
?>
Or:
<?php
// get contents of a file into a string
$filename = "/path/to/myFile";
$handle = fopen($filename, "r");
$contents = fread($handle, 15);
fclose($handle);
?>
Either method you can use byte 7-15 to do what you want. I don't think you can go after certain bytes without starting from the beginning of the file.

Working with huge text files in PHP?

I need to scan through a 30MB text file - it's a list of world cities - How can I access this file, I feel like a File_Get_Contents will give my server a stroke
Just fopen it and then use fgets.
Filesystem functions come handy in this situation.
Example
$filename = "your_file_path";
// to open file
$fp = fopen($filename, 'r'); // use 'rw' to open file in read/write mode
// to output entire file
echo fread($fp, filesize($filename));
// to close file
fclose($fp);
References
(some handy functions)
All Filesystem Functions
fopen() - open file
fread() - read file content
fgets() - to get line
fwrite() - write content to file
fseek() - change file pointer's position
rewind() - rewind file pointer to pos 0
fclose() - close file
...
<?php
$fh = #fopen("inputfile.txt", "r");
if ($fh) {
while (($line = fgets($fh)) !== false) {
echo $line;
// do something with $line..
}
fclose($fh);
}
?>
More information/examples on http://pt.php.net/manual/en/function.fgets.php

Categories