View Full Version : Php read binary file


Waldo
19-12-2008, 15:13
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. :)

Ghozer
19-12-2008, 15:18
Is there some tokeniser? like putting a comma between values in the text? if so, you could use explode()

Waldo
19-12-2008, 15:22
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?

Waldo
19-12-2008, 15:24
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++

adaline
19-12-2008, 15:36
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);

Waldo
19-12-2008, 15:38
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?

Waldo
19-12-2008, 15:41
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).

adaline
19-12-2008, 15:55
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 :)

Waldo
19-12-2008, 16:07
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?

Waldo
19-12-2008, 16:40
Sussed it. Used the unpack() function, if anyone interested..

John
19-12-2008, 17:39
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.

Waldo
19-12-2008, 17:49
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?

adaline
19-12-2008, 17:50
$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 :)

adaline
19-12-2008, 17:52
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.

Waldo
19-12-2008, 17:55
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!!! :)