monarlte Posted March 15, 2007 Share Posted March 15, 2007 Hi. I am wanting to write a php script that will output the binary value of a binary file. When I print the current chunk, I just get the following printed to the screen: "Resource id #3". And when I load the whole file contents into a string variable and then print that string, I just get jibberish printed to the screen. Could somebody please tell me how I could print the binary value of the file? Thank you. $filename = 'song.mp3'; if (file_exists($filename)){ //This will print jibberish $handle = fopen($filename, "r"); $contents = fread($handle, $filesize); fclose($handle); print $contents; print '<br /><br />'; //This next part will print "Resource id #3" $handle = fopen($filename, "r"); while(!feof($handle)) { $chunk = fread($handle, 1024); print $handle; print '<br /><br />'; } } Link to comment https://forums.phpfreaks.com/topic/42794-binary-file/ Share on other sites More sharing options...
genericnumber1 Posted March 15, 2007 Share Posted March 15, 2007 the.. binary file value.. is SUPPOSED to be "jibberish"? btw file_get_contents($filename) is simpler Link to comment https://forums.phpfreaks.com/topic/42794-binary-file/#findComment-207705 Share on other sites More sharing options...
monarlte Posted March 15, 2007 Author Share Posted March 15, 2007 I want to print the bits of the file (the 1s and 0s), how can I do that? Link to comment https://forums.phpfreaks.com/topic/42794-binary-file/#findComment-207707 Share on other sites More sharing options...
kenrbnsn Posted March 15, 2007 Share Posted March 15, 2007 Once you get the contents into a string, do something like the following: <?php $str = 'sdfasi0sdjfa09c8hds8cusd8uas8'; for ($i=0;$i<strlen($str);$i++) printf("%08b ",ord($str[$i])); ?> Ken Link to comment https://forums.phpfreaks.com/topic/42794-binary-file/#findComment-207711 Share on other sites More sharing options...
monarlte Posted March 15, 2007 Author Share Posted March 15, 2007 thanks Ken. How could I now display the binary as ASCII text characters? thanks so much. Link to comment https://forums.phpfreaks.com/topic/42794-binary-file/#findComment-207725 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 15, 2007 Share Posted March 15, 2007 What this is doing is creating binary out of the ASCII characters, Its taking the "jibberish" and converting it to binary (lol, jibberish still, huh), converting it back to ASCII would be redundant and would thus render the initial conversion uneeded. Link to comment https://forums.phpfreaks.com/topic/42794-binary-file/#findComment-207729 Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 i'm not sure i understand but you may want to look into system() if you're trying to compile binary data. Link to comment https://forums.phpfreaks.com/topic/42794-binary-file/#findComment-207750 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.