Cannot upload .csv using text/plain

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

    #1

    Cannot upload .csv using text/plain

    I am using Excel to save a spreadsheet in .csv format. This creates a
    text file with extension .csv
    But when I try to upload the file to the server by checking for its
    MIME type using $_FILE['userfile']['type'] I get my own error message
    that the file is not text file. If I change the extension to .txt I am
    able to upload the file.
    This is not reliable as it is only relying on extensions rather than
    contents of the file. Is there a reliable way of checking for file
    formats. I would like to upload jpeg, gif and text files reliably.

    Thank you
    John
  • Shawn Wilson

    #2
    Re: Cannot upload .csv using text/plain

    John wrote:[color=blue]
    >
    > I am using Excel to save a spreadsheet in .csv format. This creates a
    > text file with extension .csv
    > But when I try to upload the file to the server by checking for its
    > MIME type using $_FILE['userfile']['type'] I get my own error message
    > that the file is not text file. If I change the extension to .txt I am
    > able to upload the file.
    > This is not reliable as it is only relying on extensions rather than
    > contents of the file. Is there a reliable way of checking for file
    > formats. I would like to upload jpeg, gif and text files reliably.[/color]

    For gif and jpeg, you can use getimagesize() (the 3rd or 4th array element, I
    think). Not sure how you'd go about validating a CSV though.

    Regards,
    Shawn
    --
    Shawn Wilson
    shawn@glassgian t.com

    Comment

    • John Dunlop

      #3
      Re: Cannot upload .csv using text/plain

      John wrote:
      [color=blue]
      > I am using Excel to save a spreadsheet in .csv format. This creates a
      > text file with extension .csv
      > But when I try to upload the file to the server by checking for its
      > MIME type using $_FILE['userfile']['type'][/color]

      I trust you meant $_FILES.
      [color=blue]
      > I get my own error message that the file is not text file.[/color]

      The value of $_FILES['userfile']['type'] is whatever the file's media
      type was deemed to be at the client-side, if such deeming took place
      at all; as the Content-Type header is optional in multipart types
      (and data sets of "file upload forms" ought to be sent using
      multipart/form-data), a file's media type may be absent from the
      request -- you couldn't, then, figure out the file's media type from
      $_FILES. Morever, if a browser tries but is unable to determine an
      appropriate media type, it should fall back on the oft-misused
      application/octet-stream type (RFC2388, sec. 3), i.e., labelled the
      entity body "arbitrary binary data" (RFC2046, sec. 4.5.1).

      Internet Explorer 6.0, for example, sends a Content-Type value of
      application/octet-stream with CSV files, which is fine; Opera 7.10
      sends text/comma-separated-values, which is curious since that media
      type isn't registered. There is, in fact, no registered media type
      for CSV files. Many transactions are conducted with text/x-csv or
      other private subtypes.

      I wonder how a TSV (Tab Separated Values, media type text/tab-
      separated-values) file would bear up.
      [color=blue]
      > If I change the extension to .txt I am able to upload the file. This
      > is not reliable as it is only relying on extensions rather than
      > contents of the file.[/color]

      There is no normative source limiting the determining of files' media
      types from filename extensions. Manifold heuristics may be employed.
      [color=blue]
      > Is there a reliable way of checking for file formats.[/color]

      Not "bullet proof",

      PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


      [color=blue]
      > I would like to upload jpeg, gif and text files reliably.[/color]

      Shawn's already proposed getimagesize for JPEGs and GIFs.



      --
      Jock

      Comment

      Working...