PHP Terms Files Binary Append

 

- Info:

- This tutorial shows how to use PHP to append binary data.

- If file exists data is appended to the end of the file.

- If file doesn't exists new one is created.

- File pointer is at the end of the file.

 

- Procedure:

- Binary data 10 11 is added to the end of file C:\inetpub\wwwroot\test.bin.

- If such file doesn't already exist new one is created and data is saved into it.

- Create PHP Console Application Test.php.

 

test.txt (Initial HEX values)

78 56 34 12 41 42 12 34 56 78

 

Test.php

<?php

$fp = fopen("test.txt", 'ab'); //Create FilePointer for appending to file.

if($fp==false) {

print("Unable to open or create file.");

exit(1);

}

flock ($fp, LOCK_EX);

$binaryData = pack("c*", 16, 17);

fwrite($fp, $binaryData);

flock ($fp, LOCK_UN);

fclose($fp);

?>

 

test.txt (Resulting HEX values after appending)

78 56 34 12 41 42 12 34 56 78 10 11