Determining filetype

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schraalhans Keukenmeester

    Determining filetype

    Is it possible in PHP to determine whether a file is (intended as)
    binary or ascii data other than by examining its extension? I can't find
    a ready-made function or script online that does so.

    If I open a file with less on my linux box for example it warns when it
    'thinks' it's binary rather than ascii.

    TIA.
    Sh.
  • Arjen

    #2
    Re: Determining filetype

    Schraalhans Keukenmeester schreef:
    Is it possible in PHP to determine whether a file is (intended as)
    binary or ascii data other than by examining its extension? I can't find
    a ready-made function or script online that does so.
    No, one could make an educated guess however for the types u are expecting.

    --
    Arjen
    HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

    Comment

    • petersprc

      #3
      Re: Determining filetype

      Hi,

      The "file" command on unix should tell you if the file is text or
      binary.

      You could also do it with a regexp:

      $isBinary = preg_match('/[^\x08\t\n\r\f\x 20-\x7e]/',
      file_get_conten ts('/the/file'));

      (By the way, less can be made to stop warning you by using the -f
      option: less -f /usr/local/bin/php)

      On Feb 17, 2:15 pm, Schraalhans Keukenmeester <bitbuc...@inva lid.spam>
      wrote:
      Is it possible in PHP to determine whether a file is (intended as)
      binary or ascii data other than by examining its extension? I can't find
      a ready-made function or script online that does so.
      >
      If I open a file with less on my linux box for example it warns when it
      'thinks' it's binary rather than ascii.
      >
      TIA.
      Sh.

      Comment

      • Gordon Burditt

        #4
        Re: Determining filetype

        >Is it possible in PHP to determine whether a file is (intended as)
        >binary or ascii data other than by examining its extension? I can't find
        >a ready-made function or script online that does so.
        >
        >If I open a file with less on my linux box for example it warns when it
        >'thinks' it's binary rather than ascii.
        An 'ascii' file probably does not contain control characters (codes
        0 - 31 or 127) other than tab, carriage return, line feed, possibly
        form feed, and possibly backspace. Depending on the character set,
        it may not contain codes 128 - 255 either.

        Comment

        • Colin McKinnon

          #5
          Re: Determining filetype

          Schraalhans Keukenmeester wrote:
          Is it possible in PHP to determine whether a file is (intended as)
          binary or ascii data other than by examining its extension? I can't find
          a ready-made function or script online that does so.
          >
          If I open a file with less on my linux box for example it warns when it
          'thinks' it's binary rather than ascii.
          >
          The 'file' command from the shell prompt will give you the file type. There
          are a wealth of tools for handling files available from the shell you might
          want to read the man pages for 'cat', 'tr' and 'strings' too.

          HTH

          C.

          Comment

          Working...