Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Creating file headers in php.

Creating file headers in php.

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
phphelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Danzy83
    wrote on last edited by
    #1

    Hello experts. Is it possible to create file headers in PHP? In C, I could rely on size of data types to create file headers so that if I want a specific information from the file header, I had to seek to the appropriate byte location and read what I wanted. I am seeing this to be difficult in PHP. I don't know how I can save, for instance, the length of a string in a file header and later retrieve it at a known byte location. I started with something like the following:

    class FileHeader
    {
    private $contentSize;
    private $titleLength;
    private $headerLength;
    }

    Because the variables can take different data types in PHP, I don't know how I can read the header information before dealing with the file. I can't tell the length that will take me to, for instance, $headerLength in the file so that I can read it since I wouldn't know the length of a particular data. It seems the number of bytes can vary for different headers of different files. Actually, I am a newbie in PHP and I don't know if there is any other way that will make such a task possible. Please help.

    C N 2 Replies Last reply
    0
    • D Danzy83

      Hello experts. Is it possible to create file headers in PHP? In C, I could rely on size of data types to create file headers so that if I want a specific information from the file header, I had to seek to the appropriate byte location and read what I wanted. I am seeing this to be difficult in PHP. I don't know how I can save, for instance, the length of a string in a file header and later retrieve it at a known byte location. I started with something like the following:

      class FileHeader
      {
      private $contentSize;
      private $titleLength;
      private $headerLength;
      }

      Because the variables can take different data types in PHP, I don't know how I can read the header information before dealing with the file. I can't tell the length that will take me to, for instance, $headerLength in the file so that I can read it since I wouldn't know the length of a particular data. It seems the number of bytes can vary for different headers of different files. Actually, I am a newbie in PHP and I don't know if there is any other way that will make such a task possible. Please help.

      C Offline
      C Offline
      cjoki
      wrote on last edited by
      #2

      I am not sure you can read the header with any php based functions. But maybe you can use the system(), exec(), or passthru() to call a system resource to return what you are looking for.

      Chris J www.redash.org

      1 Reply Last reply
      0
      • D Danzy83

        Hello experts. Is it possible to create file headers in PHP? In C, I could rely on size of data types to create file headers so that if I want a specific information from the file header, I had to seek to the appropriate byte location and read what I wanted. I am seeing this to be difficult in PHP. I don't know how I can save, for instance, the length of a string in a file header and later retrieve it at a known byte location. I started with something like the following:

        class FileHeader
        {
        private $contentSize;
        private $titleLength;
        private $headerLength;
        }

        Because the variables can take different data types in PHP, I don't know how I can read the header information before dealing with the file. I can't tell the length that will take me to, for instance, $headerLength in the file so that I can read it since I wouldn't know the length of a particular data. It seems the number of bytes can vary for different headers of different files. Actually, I am a newbie in PHP and I don't know if there is any other way that will make such a task possible. Please help.

        N Offline
        N Offline
        Niall Barr
        wrote on last edited by
        #3

        If you really need to work with binary files in PHP you need to use the pack and unpack functions, along with the C like file functions (fopen, fseek, fread etc.), however if you need to work with binary files you probably really need to consider whether PHP is an appropriate language for your application. Niall

        D 1 Reply Last reply
        0
        • N Niall Barr

          If you really need to work with binary files in PHP you need to use the pack and unpack functions, along with the C like file functions (fopen, fseek, fread etc.), however if you need to work with binary files you probably really need to consider whether PHP is an appropriate language for your application. Niall

          D Offline
          D Offline
          Danzy83
          wrote on last edited by
          #4

          I think I cannot use PHP for my task. I tried to write an integer value which could have occupied 4 bytes (or 2 bytes) of memory but the value was written as two sequence of characters. This means that I cannot rely on writing such values in PHP. 27 was saved as it is of two characters '2' and '7'. So 274 will also be saved as 3 characters where in C I would have assumed that to be of 4 sequence of bytes. I think PHP is unsuitable for such a task. Or maybe there are other ways around which I don't know and need to be taught.

          modified on Monday, January 31, 2011 7:58 PM

          N 1 Reply Last reply
          0
          • D Danzy83

            I think I cannot use PHP for my task. I tried to write an integer value which could have occupied 4 bytes (or 2 bytes) of memory but the value was written as two sequence of characters. This means that I cannot rely on writing such values in PHP. 27 was saved as it is of two characters '2' and '7'. So 274 will also be saved as 3 characters where in C I would have assumed that to be of 4 sequence of bytes. I think PHP is unsuitable for such a task. Or maybe there are other ways around which I don't know and need to be taught.

            modified on Monday, January 31, 2011 7:58 PM

            N Offline
            N Offline
            Niall Barr
            wrote on last edited by
            #5

            This bit of code shows you how to write an integer to an ASCII text file, then (as a four byte long) to a binary file. The key thing is that pack is used to convert the integer into a character array equivalent to its binary representation before writing it. The default behavior of PHP when writing variables to a file is to convert them to strings.

            D 1 Reply Last reply
            0
            • N Niall Barr

              This bit of code shows you how to write an integer to an ASCII text file, then (as a four byte long) to a binary file. The key thing is that pack is used to convert the integer into a character array equivalent to its binary representation before writing it. The default behavior of PHP when writing variables to a file is to convert them to strings.

              D Offline
              D Offline
              Danzy83
              wrote on last edited by
              #6

              Narr, it works. Now I believe reading the file requires

              unpack()

              function. I tried to read about this function but I get confused with the format that is to be specified to it but I am trying to understand it. Thanks a lot.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups