By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,594 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

read a file n convert it to its hexadecimal equivalent

nabanita
P: 9
is there any way in which i could open a file in php to read a file n convert it to its hexadecimal equivalent..
Mar 15 '10 #1

✓ answered by philipwayne

The above is not complete it reads the actual files contents but does not convert it to hexadecimal.

Expand|Select|Wrap|Line Numbers
  1. $filename = "c:\\files\\somepic.gif";
  2. $handle = fopen($filename, "rb");
  3. $contents = bin2hex( fread( $handle, filesize($filename) ) );
  4. fclose($handle);
  5.  
EDIT:
Oh and use
Expand|Select|Wrap|Line Numbers
  1. pack( "H*", $contents );
to convert it back.

Share this Question
Share on Google+
2 Replies


RedSon
Expert 5K+
P: 5,000
See example #2:
http://php.net/manual/en/function.fread.php

It's wondrous what a few minutes googling will find you.
Mar 15 '10 #2

P: 50
The above is not complete it reads the actual files contents but does not convert it to hexadecimal.

Expand|Select|Wrap|Line Numbers
  1. $filename = "c:\\files\\somepic.gif";
  2. $handle = fopen($filename, "rb");
  3. $contents = bin2hex( fread( $handle, filesize($filename) ) );
  4. fclose($handle);
  5.  
EDIT:
Oh and use
Expand|Select|Wrap|Line Numbers
  1. pack( "H*", $contents );
to convert it back.
Mar 15 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.