Reading Binary Files in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ihere
    New Member
    • Jun 2007
    • 5

    Reading Binary Files in PHP

    Recently I need to write a PHP script that would process a binary file I had created in my VB program. Can PHP do this? and how?
    PS: Sorry for my bad English.
  • jx2
    New Member
    • Feb 2007
    • 228

    #2
    dont worry about your english :-) its better then mine :-)

    u miht want to see this http://us3.php.net/manual/en/function.fopen. php
    simple use fopen("urefile" ,'rb'); - 'b' for binary

    Comment

    • ihere
      New Member
      • Jun 2007
      • 5

      #3
      Originally posted by jx2
      dont worry about your english :-) its better then mine :-)

      u miht want to see this http://us3.php.net/manual/en/function.fopen. php
      simple use fopen("urefile" ,'rb'); - 'b' for binary
      It is not so simple as you think. I have a binary file test.qbt that I had created in my VB6 program. In this binary file: simple text, picture and expression. Now i have to read all data from that file and write to browser (text, picture and expression). I dont know how? Please help me!!!!

      Comment

      • jx2
        New Member
        • Feb 2007
        • 228

        #4
        [PHP]
        <?php
        session_start() ;
        $fp=fopen("p1.p hp",'rb');
        $i=0;
        do{
        fseek($fp,$i);
        $buff=fread($fp ,1);

        echo ord($buff))."<b r>\n";
        $i++;
        }while($i<=file size('p1.php')) ;
        [/PHP]

        that is the way to do it-(i hope :-)
        it reads file byte by byte and displays as binary

        by the way how u going to use those images?
        i hope it helps
        jx2

        Comment

        • jx2
          New Member
          • Feb 2007
          • 228

          #5
          of course p1.php its only as an example :-)
          let me know if that is what you wanted...

          Comment

          • ihere
            New Member
            • Jun 2007
            • 5

            #6
            Originally posted by jx2
            of course p1.php its only as an example :-)
            let me know if that is what you wanted...
            No i have a test file: click here: http://www.nguyentiendat.narod.ru/test.qbt that i had created in VB6. Now i have to read it from PHP and show all data in browser (include text, picture,...). I dont know how....

            Comment

            • jx2
              New Member
              • Feb 2007
              • 228

              #7
              Originally posted by ihere
              No i have a test file: click here: http://www.nguyentiendat.narod.ru/test.qbt that i had created in VB6. Now i have to read it from PHP and show all data in browser (include text, picture,...). I dont know how....
              but how the structure of file looks like? how did you created it? i gues its a kind of the databases...

              Comment

              • jx2
                New Member
                • Feb 2007
                • 228

                #8
                Originally posted by jx2
                but how the structure of file looks like? how did you created it? i gues its a kind of the databases...
                i'll be back in the evening :-) i c what is it :-) cool :-)

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Changed thread title: Removed superfluous '????' and 'help'.

                  Comment

                  • ihere
                    New Member
                    • Jun 2007
                    • 5

                    #10
                    Originally posted by jx2
                    but how the structure of file looks like? how did you created it? i gues its a kind of the databases...
                    Look at: http://www.nguyentiendat.narod.ru/testfile.pdf Right click and choose Save Target As
                    I'm looking for your answer

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Please do not bump your posts. Thanks!

                      Comment

                      • Motoma
                        Recognized Expert Specialist
                        • Jan 2007
                        • 3236

                        #12
                        You will need to know the file format structure of the binary file you are using. Do you?

                        Comment

                        • ihere
                          New Member
                          • Jun 2007
                          • 5

                          #13
                          Originally posted by Motoma
                          You will need to know the file format structure of the binary file you are using. Do you?
                          Of cause i know, but i dont know how to transfer data from file to normal data for showing on browse, example picture,...

                          Comment

                          • bucabay
                            New Member
                            • Apr 2007
                            • 18

                            #14
                            Originally posted by ihere
                            Of cause i know, but i dont know how to transfer data from file to normal data for showing on browse, example picture,...
                            You have to send the right HTTP Headers that define the Content-Type.. eg:
                            [PHP]
                            header('Content-Type: image/png');[/PHP]

                            If you image is a png image.

                            Then read the binary image data from teh file and output it in the HTTP Body.

                            [PHP]
                            // send HTTP headers
                            header('Content-Type: image/png');
                            header('Content-Length: '.filesize($bin ary_file_path)) ;

                            // send HTTP Body
                            $fp = fopen($binary_f ile_path, 'rb'); // open file pointer
                            fpassthru($fp); // dump file stream to http
                            fclose($fp);

                            [/PHP]

                            Comment

                            • twinklyblue
                              New Member
                              • Jun 2007
                              • 37

                              #15
                              Originally posted by bucabay
                              You have to send the right HTTP Headers that define the Content-Type.. eg:
                              [PHP]
                              header('Content-Type: image/png');[/PHP]

                              If you image is a png image.

                              Then read the binary image data from teh file and output it in the HTTP Body.

                              [PHP]
                              // send HTTP headers
                              header('Content-Type: image/png');
                              header('Content-Length: '.filesize($bin ary_file_path)) ;

                              // send HTTP Body
                              $fp = fopen($binary_f ile_path, 'rb'); // open file pointer
                              fpassthru($fp); // dump file stream to http
                              fclose($fp);

                              [/PHP]
                              I think this is a stupid question..but how do you dump it in the http body? I cant seem to make it appear in my webpage. All I get is a corrupted file. Thanks for your reply!

                              Comment

                              Working...