View Full Version : Php read binary file
Hi,
I'm reading a file from my server:
$contents = file_get_contents($_FILES["file"]["tmp_name"]);
the contents are there ok, the file is 16 bytes in size; and contains 4 * 32bit values.
I want to put these in to variables;
$v1 = ?
$v2 = ?
$v3 = ?
$v4 = ?
How do I do that in PHP?
Thanks. :)
Is there some tokeniser? like putting a comma between values in the text? if so, you could use explode()
Nope; it's just pure binary data mate. I can change the format (bit of messing around) but if it's simple to just read it in PHP, it's current state, I'd prefer that.
Even if I put a comma in there, would PHP still make sense of it?
Can I not just cast it as an array of longs (like you would in c/c++) .... or maybe PHP isn't *that* simular to c/c++
Nah i dont think you can cast it into an array. If its just one long string of 4*4bytes, you could split it up into seperate values using substr roughly like so:
$v1 = substr($contents,0,4);
$v2 = substr($contents,4,4);
$v3 = substr($contents,8,4);
$v4 = substr($contents,12,4);
Can I use f_open() and f_read()?
I could to 4 * f_read(), each time reading in 4 bytes of binary data.
Would that work?
It's not really strings adaline. It's raw data, for example, the 4 data items could be:
0xf40900ea
0x003948fe
0xa0be9fda
0x0000928f
So that's 4 * 32bit values (expressed here, in Hex).
Basically you can read the whole file, it will end up as a binary string in a variable (eg $content)
Then split the string into separate variables, i dont think many php developers do what you do - so its a case of trying it out, seeing what works for you :)
Don't think it's going to work mate. I need to change the format of the file, to use string data..
i.e. Something like, '0,324444,2344,223235535'
How do I explode that (in to 4 variables), as per Ghozers' suggestions?
Sussed it. Used the unpack() function, if anyone interested..
I realise Waldo got the correct solution.
$v1 = substr($contents,0,4);
Should be: (untested but you get the idea)
$v1 = (ord(substr($contents,0,1)) << 24) + (ord(substr($contents,1,1)) << 16) + (ord(substr($contents,2,1)) << 8) + ord(substr($contents,3,1));
Otherwise you just end up with what looks like a corrupted string.
While we're at it...
I've now got a normal string of data; that I want to send back to the users PC as a file (they can download).
So, say I have...
$mystring = "hello you have just downloaded the file containing this string".
How do I force the browser to init a file download?
$v1 = (ord(substr($contents,0,1)) << 24) + (ord(substr($contents,1,1)) << 16) + (ord(substr($contents,2,1)) << 8) + ord(substr($contents,3,1));
Otherwise you just end up with what looks like a corrupted string.
Ah yes shifting values, its all coming back to me now :)
So, say I have...
$mystring = "hello you have just downloaded the file containing this string".
How do I force the browser to init a file download?
http://elouai.com/force-download.php
Just echo the value instead of the readfile call.
I have to save the string as a file first? If so, what's the best practice for creating a temporary file from a string?
EDIT: Sorry to be a pain; you guys are AWESOME!!! :)
|
|