php hexdecimal conversion of png file - php

I am trying to get the image header from a png file. as the binary content show as neeble how can i show it as hexadecimal? i tried to base_convert() but it is not returning the desired output as i am getting the poutput in hexadecimel editor. any help will be appreciated.I am trying to do it with php

Try this for size:
$filename = "C:\\wamp\\www\\projects\\cog.png";
$handle = fopen($filename, "rb");
$binContents = fread($handle, filesize($filename));
fclose($handle);
$hexContents = bin2hex($binContents);
echo $hexContents;

Related

How to convert Excel file to text file in php?

I want to convert an excel file to a text file. I have tried this code but it doesn't work:
$destination = 'products.xls';
$section = file_get_contents($destination);
$File = "file.txt";
$File = str_replace(';','#',$File);
$Handle = fopen($File, 'w');
$Data = $section;
fwrite($Handle, $Data);
print "Data Written"; die;
When I open the text file it shows me special characters and when I convert csv to text it shows me the right result. Please provide any helpful links.
You cannot read the Excel file that way. Excel file is binary file that follows the XLS format while file_get_contents is for pure text file. You need a library (like https://github.com/PHPOffice/PHPExcel) to read the content then write to text.

read the contents from pdf and neatly align the details

How to read the contents from pdf and neatly align the details in html page using php
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
print $contents;
But some unwanted symbols are coming
Best way to use PDF2TEXT:
//Include here
include('class.pdf2text.php');
$var = new PDF2Text();
//Set PDF
$var->setFilename('YOUR PDF.pdf');
$var->decodePDF();
//Desired Output
echo $var->output();

Fopen problems in php

In PHP im creating a tool to open txt file with an integer in it, increment the number, save the number to a variable and then save and close the file. this is the code i have for it and it doesnt seem to work when i have it on my test server. Can anyone clue me in as to why this isnt working properly?
//opens pclnumber.txt to $handle, saves number to $number, Increments number in text file, saves and closes file
$handle = fopen("pclnumber.txt", "w+");
$number = fread($handle);
fwrite($handle, $number+1);
fclose($handle);
over all you must set reading permission on your file, and than you can use the follow code:
$filename = "pclnumber.txt";
//read file content
$handle = fopen($filename, "r");
$number = fread($handle, sizeof($filename));
fclose($handle);
//update file content
$handleWrite = fopen($filename, "w+");
fwrite($handleWrite, $number+1);
fclose($handleWrite);
Bye,
Marco

Extracting text between values in txt file using php

Im trying to extract the values between eng_tid
and eng_data for http://fdguirhgeruih.x10.mx/html.txt and I keep getting T string errors.
why do I keep getting errors
<? php
//First, open the file. Change your filename
$file = "http://fdguirhgeruih.x10.mx/html.txt";
$word1='tid';
$word2='data';
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
$between=substr($contents, strpos($contents, $word1), strpos($contents, $word2) - strpos($contents, $word1));
echo $between;
?>
UPDATE after seeing error messages...
Instead of fread() and attempting to use the size in bytes of your target file, you may simply use file_get_contents() to retrieve the remote file. Your error is because PHP wants to read the filesize of the file as though it is local, but it is a remote file over HTTP. filesize() reports 0 and an error. Instead do
// Don't do this...
//$handle = fopen($file, "r");
//$contents = fread($handle, filesize($file));
//fclose($handle);
// Instead do this...
$contents = file_get_contents($file);

PHP: Uncompressing gzcompressed file with gzuncompress and fopen

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.

Categories