Jump to content

Writing binary files such as image files


suikki

Recommended Posts

I cannot find how to write a binary file such as as data into a .bmp image file.I tried the following but it simply writes ascii bytes of the characters instead of their binary value $head[0]= "1";$head[1]= "12";$head [2] ="34"; $filein=fopen('code.bmp','wb'); //File to be convertedfor ($i=0; $i < 3; ++$i) {fputs ($filein,$head[$i],2);}fclose($filein); Chuck S.

Link to comment
Share on other sites

Solution found:$head[0]= dechex("120");$head[1]= dechex("112");$head [2] = dechex("64");if (!unlink('code.bmp')) echo "Could not delete this file"; else echo "new file";$filein=fopen('code.bmp','wb'); //File to be convertedfor ($i=0; $i < 3; ++$i) {fputs ($filein,$head[$i],2);}fclose($filein); thanks for your comments

Link to comment
Share on other sites

That sounds weird. If you want to write binary characters you should use the chr() method. Using dechex() is just going to print the hexadecimal digits in string form into the file, literally characters like "5" and "F".

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...