JeremyCanada26 Posted June 2, 2010 Share Posted June 2, 2010 I want to get a bytearray from a temp file(uploaded file) on my system. I need the bytearray in order to calculate a crc on it to see if all of the data is the same as before I sent it from the client. an example of the temp name would be /tmp/g23uh4 Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/ Share on other sites More sharing options...
premiso Posted June 2, 2010 Share Posted June 2, 2010 $file = '/tmp/g23uh4'; $file_checksum = crc32(file_get_contents($file)); $checksum = 'sum grabbed from source'; if ($checksum == $file_checksum) { echo 'file is valid'; }else { echo 'file is not valid'; } Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066759 Share on other sites More sharing options...
JeremyCanada26 Posted June 2, 2010 Author Share Posted June 2, 2010 $file = '/tmp/g23uh4'; $file_checksum = crc32(file_get_contents($file)); $checksum = 'sum grabbed from source'; if ($checksum == $file_checksum) { echo 'file is valid'; }else { echo 'file is not valid'; } I tried that file_get_contents already because I thought that would work but the it generates a checksum that doesn't match the one that I generate from flash. What is probably causing the issue I beleive is that My crc checker class from actionscript that I use, takes in a bytearray to calculate the crc and the way your doing it, takes in a string from file_get_contents(). That's why I asked about how to get a bytearray instead, maybe if I can get a byte array and try to run the crc checker on that value, it might generate the same checksum. Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066778 Share on other sites More sharing options...
sspoke Posted June 2, 2010 Share Posted June 2, 2010 Simple debugging is required then add a few print's to the first 20 bytes of the file in actionscript (flash?) then add a few to $file = file_get_contents("/tmp/g23uh4"); $byteArr = str_split($file); for($i=0;$i<20;$i++) print "$i) ".$byteArr[$i]; or maybe just try might save some time who knows $file = '/tmp/g23uh4'; $file_checksum = crc32(str_split(file_get_contents($file))); $checksum = 'sum grabbed from source'; if ($checksum == $file_checksum) { echo 'file is valid'; }else { echo 'file is not valid'; } Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066779 Share on other sites More sharing options...
premiso Posted June 2, 2010 Share Posted June 2, 2010 This may work, un-tested: $byteArray = toByteArray(file_get_contents($file)); $file_checksum = crc32(implode("", $byteArray)); function toByteArray($string) { $byteArray = str_split($string); $return = array(); foreach ($byteArray as $item) { $return[] = ord($item); } return $return; } I am not sure if this will work, but may give you some ideas on how to get it. EDIT: An alternative function code, which may be better: function toByteArray($string) { $byteArray = str_split($string); array_map('ord', $byteArray); return $byteArray; } Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066780 Share on other sites More sharing options...
JeremyCanada26 Posted June 2, 2010 Author Share Posted June 2, 2010 This may work, un-tested: $byteArray = toByteArray(file_get_contents($file)); $file_checksum = crc32(implode("", $byteArray)); function toByteArray($string) { $byteArray = str_split($string); $return = array(); foreach ($byteArray as $item) { $return[] = ord($item); } return $return; } I am not sure if this will work, but may give you some ideas on how to get it. EDIT: An alternative function code, which may be better: function toByteArray($string) { $byteArray = str_split($string); array_map('ord', $byteArray); return $byteArray; } no, it doesn't but thanks. your method returns to me a very large negative number Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066782 Share on other sites More sharing options...
JeremyCanada26 Posted June 2, 2010 Author Share Posted June 2, 2010 I found php file() which says it returns an array of a file, assuming it's a bytearray I thought it might work but it also returns a different checksum when used. $checksum = crc32(file($file)); Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066783 Share on other sites More sharing options...
sspoke Posted June 2, 2010 Share Posted June 2, 2010 did you try my idea? ah yes try file() seems to be the solution my bad like i said try to compare the first 20 bytes of your file in php and actionscript see if they are identical if they are then it's probably crc32 function which is flawed or different varient Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066784 Share on other sites More sharing options...
JeremyCanada26 Posted June 2, 2010 Author Share Posted June 2, 2010 Simple debugging is required then add a few print's to the first 20 bytes of the file in actionscript (flash?) then add a few to $file = file_get_contents("/tmp/g23uh4"); $byteArr = str_split($file); for($i=0;$i<20;$i++) print "$i) ".$byteArr[$i]; or maybe just try might save some time who knows $file = '/tmp/g23uh4'; $file_checksum = crc32(str_split(file_get_contents($file))); $checksum = 'sum grabbed from source'; if ($checksum == $file_checksum) { echo 'file is valid'; }else { echo 'file is not valid'; } Ok, here are the results from my debugging. In actionscript code, when I output using, trace(pngByteArray.readMultiByte(40, "utf-8")); I receive the following output, http://pastebin.com/jzifWQvg I pastebinned it because it looks more like the output in the pastebin site except it's missing the white space. In php when I output using your code above, I get the following output 0) ‰1) P2) N3) G4) 5) 6) 7) Just in case anyone wants to have a look at the actionscript ByteArray class, here it is, http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/ByteArray.html Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066846 Share on other sites More sharing options...
Daniel0 Posted June 2, 2010 Share Posted June 2, 2010 crc32 returns a signed integer. Try converting it to its unsigned string representation as the example in the manual shows. Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066894 Share on other sites More sharing options...
JeremyCanada26 Posted June 2, 2010 Author Share Posted June 2, 2010 crc32 returns a signed integer. Try converting it to its unsigned string representation as the example in the manual shows. I tried that one and it was also not returning the same checksum. I switched from crc32 to md5 to see if that would work but it also generates a different value so that must mean maybe that the byteArray from my actionscript doesn't match the one from php's file_get_contents($file) that i'm using Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066917 Share on other sites More sharing options...
Daniel0 Posted June 2, 2010 Share Posted June 2, 2010 Perhaps you could show your AS code? Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066920 Share on other sites More sharing options...
JeremyCanada26 Posted June 2, 2010 Author Share Posted June 2, 2010 I should mention something that I noticed that looked kinda weird, when using the printf("%u\n", $checksum), I usually get output with the number 4, or 8 or some really small number like that Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066922 Share on other sites More sharing options...
JeremyCanada26 Posted June 2, 2010 Author Share Posted June 2, 2010 //store the png bytes into the byteArray var pngByteArray:ByteArray = PNGEncoder.encode(pngSourceBitmapData); //initialize the CRC checker var crc32Checker:CRC32 = new CRC32(); //calculate the crc of the pngByteArray crc32Checker.update(pngByteArray); //lets try md5 if we cant get crc to work? var md5String:String = MD5.hashBinary(pngByteArray); //create a parameters object var parametersObject:Object = new Object(); //add values to send along to php parametersObject.msg_to_php = "hi there"; //store the crc value of the pngByteArray parametersObject.c = crc32Checker.getValue(); //parametersObject.c = md5String; debugPanel.writeLine("loop through parameters to confirm they exist"); for (var found1:String in parametersObject) { debugPanel.writeLine("name: " + found1 + " value: " + parametersObject[found1]); } //set the position for the pointer in the ByteArray back to 0 pngByteArray.position = 0; //attempt to read the first 20 bytes debugPanel.writeLine("first 20 bytes" + pngByteArray.readMultiByte(20, "utf-8")); Above is my actionscript code that I'm currently using. Quote Link to comment https://forums.phpfreaks.com/topic/203658-how-to-get-a-bytearray-for-an-image-on-my-filesystem/#findComment-1066924 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.