www.webdeveloper.com
Results 1 to 4 of 4

Thread: binary write to file problem in mode r+b

  1. #1
    Join Date
    Dec 2013
    Posts
    5

    binary write to file problem in mode r+b

    I created a class which should creates new binary file (called 4.ip) in the case that the file does not exist. Method reset() does the job. Then I try to add some numbers from Ip to the file. I try to use binary mode but It looks like it corrupts the file. First value 0E is written correctly. I have marked the BOM which was created in the reset method (idk if is this right when it should be binary file.


    before error


    To see the above, uncomment the die(); on line 413. Then to see the bellow, comment it back.


    When I try to write the second value (line 415), the file looks corrupted, and all the 0000 disappear.


    error on second fwrite


    This is the source code

    please see the lines 412-418.

    PHP Code:
    <?php
    include("scan_ips_class.php");
    $scanIPs = new Scan_ips;
    ?>
    Please help because I really cannot find out solution, and I am lost. Trying to solve this few days.

  2. #2
    Join Date
    Dec 2013
    Posts
    5
    Yet I show you working version which is simplified. This uses no class:
    http://paste.ofcode.org/8ipwC6eWGPxZpHGK5DQFmg

    so you can see how it works. The written data do not corrupt the file. But why I cannot use the class to do that?

  3. #3
    Join Date
    Oct 2013
    Posts
    30
    have you tried the pack php function? you can see a demo: http://webtrick101.blogspot.com/2011...rray-data.html

  4. #4
    Join Date
    Dec 2013
    Posts
    5
    I did check it before, but did not understand how it works. From the code on the link you posted what does mean the values 0x47, 0x49, 0x46 ? It is binary format? But it is not clear to me what does this:
    pack("C", $data);
    does it join the binary data?

    I was used to join binary data like this: chr(64) but it calles a function. So this is faster? And why not do it like this:
    0x47 . 0x49 . 0x46 or foreach ($array as $binval) $str.=0x49 ???

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles