Page 1 of 1

problem to write to file - mode (r+b)

Posted: Sat Dec 07, 2013 3:03 pm
by murfy
Hi, can you help with this?
http://pastebin.com/VHM3QeYW

Code: Select all

<?php
include("scan_ips_class.php");
$scanIPs = new Scan_ips;
?>
You can see that I am trying to find position to read and write one byte. When you will run the index.php, it is in debug and testing mode so file 4.ip will be created. You can then reload the index.php few times to see that every time, there is written value into begin of the file. The value is binary 0100. Ok, but I would like to write next values on line 97 and 106 later when a bug should be fixed. The values are taken from array $arr which is got from ip.

Code: Select all

$fh = @fopen($this->file,"r+b");
...
fseek ( $fh , $i );
...
$byte = fread($fh,1);
...
if ( $byte === "\x00" ){
fseek($fh, $i );
echo "<h1>$i</h1>";
fwrite($fh, chr($arr[0]), 1);
echo "<h1>".($i+$this->rowLength)."</h1>";
fseek($fh, $i+$this->rowLength );
echo "<h1>".($i+2*$this->rowLength)."</h1>";
fwrite($fh, chr($arr[1]), 1);
$result = fseek($fh, $i+2*$this->rowLength );
if ( $result !== 0)
die("Error while looking for last position: " . ($i+2*$this->rowLength) . "
; result $result");
die(); // stop because some bug is here which corrupts the file
fwrite($fh, chr($arr[2]), 1);
fclose ($fh);
echo "close and exit";
break;
}
How to correct this to write with all those fwrite commands? How to correct this to have the bytes written not only on begin of the file, but also in the "middle". Why it fails to write the other bytes?

Re: problem to write to file - mode (r+b)

Posted: Sat Dec 07, 2013 6:42 pm
by requinix
What's the question?

Re: problem to write to file - mode (r+b)

Posted: Sun Dec 08, 2013 4:37 am
by murfy
How to correct this to write with all those fwrite commands? How to correct this to have the bytes written not only on begin of the file, but also in the "middle". Why it fails to write the other bytes?

Re: problem to write to file - mode (r+b)

Posted: Sun Dec 08, 2013 4:43 am
by requinix
It looks to me like it's writing fine... Are you sure it's not writing at all rather than writing but corrupting the file?

Re: problem to write to file - mode (r+b)

Posted: Sun Dec 08, 2013 6:31 am
by murfy
This post is discarded, because I was wrong!

Re: problem to write to file - mode (r+b)

Posted: Sun Dec 08, 2013 10:33 am
by murfy
I think you were right! I was blinded because I did not see the values which were really written. I think I is correct.

May I ask you for a advice yet? Do you know some mechanism how to protect the write process when two write process (threads) would try to write data at same time? Maybe I should solve atomicity. But I don't have any link. I have seen some code in Nette frame work, but I don't use any framework and actually I think I will run the code on php 4.3.4 (I want use freehosting and they have only php 4.3.4 and on some domains maybe php 5.3, so I don't know yet which version I will have).

Thanks

Re: problem to write to file - mode (r+b)

Posted: Sun Dec 08, 2013 6:06 pm
by requinix
File locking. You could also load the file into memory and write it out (both operations being atomic) but you'd have to check the file for modifications before writing, and then race conditions, and then you're back to locking the file again.

Re: problem to write to file - mode (r+b)

Posted: Mon Dec 09, 2013 5:24 am
by murfy
Hi again, I would need help again with the thing which was resolved, but suddenly it stopped working after I did some changes (I added method readAll). Or is it just error in the editor I use to view binary code?

The old link should show how it worked before.

Now the link with code which fails:
http://pastebin.com/16U7pGXk

Go to line 413 - 417, I place there a test block // TEST
which causes some odd behaviour. add die() before the TEST block. delete old file 4.ip. reload index.php . Then you see that the file 4.ip is successfully created and the binary content is all right. But if you remove the die command, then the test block is executed and in the hexadecimal editor it looks like there are only two values: 0ECD . The rest of the file is not visible. But when I check the file it has size 187kB which looks ok. If you dont find error, you can remove the test block and test what it does. You can tell me if you see the same thing as me.

Thanks

Re: problem to write to file - mode (r+b)

Posted: Mon Dec 09, 2013 5:03 pm
by murfy
This is how it looks before it is changed. It contains unicode BOM - have no idea why. I want to write to binary and it dismiss when I write on second try. Then it looks like corrupted in the hexa editor.

Image

And this is how it looks when it is written on the second try:
Image

This is the relevant part:

Code: Select all

if ( !$this->FRWBHandler )
	$this->FRWBHandler = fopen($this->file,"r+b");
$fh =& $this->FRWBHandler;
for($i=$segmentBegin; $i<=$segmentBegin+$this->segmentLength; $i++):
	fseek( $fh  , $i );
//	echo $i . " ";
	$byte = fread($fh,1);
//	echo (" <b>$i:$byte</b><br>");
	if ( $byte === "\x00" ){
	    fseek($fh, $i);
	// echo "<h2>".$arr[1]." $i</h2>";
	fwrite($fh, chr($arr[1])); // THIS WAS OK

	// TEST
	$result = fseek($fh, $i+2 );
	fwrite($fh, chr($arr[2])); // THIS FAILS or CORRUPTS FILE
	fclose($fh);
	die($arr[1]." ".$arr[2]." ".$arr[3]. " result $result");
.....