Upload file format checking

Discussion in 'Perl Misc' started by Bryan, Mar 2, 2006.

  1. Bryan

    Bryan Guest

    Hi,

    What is the best way to validate that an uploaded file (using CGI.pm) is
    a tab delimited table of the correct format? I.e. every line has the
    same number of columns as the header, and the header has to match a
    predefined set of table headers. Should it be done as the file is read
    in? After I store it to disk?

    Suggestions for implementations?

    Thanks,
    B
     
    Bryan, Mar 2, 2006
    #1
    1. Advertising

  2. Bryan

    Guest

    Bryan <> wrote:
    > Hi,
    >
    > What is the best way to validate that an uploaded file (using CGI.pm) is
    > a tab delimited table of the correct format? I.e. every line has the
    > same number of columns as the header, and the header has to match a
    > predefined set of table headers. Should it be done as the file is read
    > in? After I store it to disk?


    That depends on what you plan to do about it if the file is not valid,
    and what you plan to do with it if it is valid.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Mar 2, 2006
    #2
    1. Advertising

  3. Bryan wrote:
    > What is the best way to validate that an uploaded file (using CGI.pm) is
    > a tab delimited table of the correct format? I.e. every line has the
    > same number of columns as the header, and the header has to match a
    > predefined set of table headers. Should it be done as the file is read
    > in? After I store it to disk?


    I don't understand how you would be able to validate the file before it
    has been stored to disk, at least temporarily. So my answer would be:
    After you have stored it, and then delete the file if the validation fails.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 2, 2006
    #3
  4. Bryan

    Bryan Guest

    wrote:
    > Bryan <> wrote:
    >
    >>Hi,
    >>
    >>What is the best way to validate that an uploaded file (using CGI.pm) is
    >>a tab delimited table of the correct format? I.e. every line has the
    >>same number of columns as the header, and the header has to match a
    >>predefined set of table headers. Should it be done as the file is read
    >>in? After I store it to disk?

    >
    >
    > That depends on what you plan to do about it if the file is not valid,
    > and what you plan to do with it if it is valid.
    >
    > Xho
    >


    Dont really see how that matters as that is another function. Maybe Ill
    delete it. Maybe Ill fire a js message, maybe Ill fix it.

    But I think a yes or no would be sufficient, a function that returns a 1
    or 0.
     
    Bryan, Mar 2, 2006
    #4
  5. Bryan <> wrote in
    news:57tNf.55611$:

    > wrote:
    >> Bryan <> wrote:
    >>
    >>>Hi,
    >>>
    >>>What is the best way to validate that an uploaded file (using CGI.pm)
    >>>is a tab delimited table of the correct format? I.e. every line has
    >>>the same number of columns as the header, and the header has to match
    >>>a predefined set of table headers. Should it be done as the file is
    >>>read in? After I store it to disk?

    >>
    >>
    >> That depends on what you plan to do about it if the file is not
    >> valid, and what you plan to do with it if it is valid.
    >>
    >> Xho
    >>

    >
    > Dont really see how that matters as that is another function. Maybe
    > Ill delete it. Maybe Ill fire a js message, maybe Ill fix it.
    >
    > But I think a yes or no would be sufficient, a function that returns a
    > 1 or 0.


    Are you somehow expecting that someone is going to write that for you?

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Mar 2, 2006
    #5
  6. Bryan

    Guest

    Bryan <> wrote:
    > wrote:
    > > Bryan <> wrote:
    > >
    > >>Hi,
    > >>
    > >>What is the best way to validate that an uploaded file (using CGI.pm)
    > >>is a tab delimited table of the correct format? I.e. every line has
    > >>the same number of columns as the header, and the header has to match a
    > >>predefined set of table headers. Should it be done as the file is read
    > >>in? After I store it to disk?

    > >
    > >
    > > That depends on what you plan to do about it if the file is not valid,
    > > and what you plan to do with it if it is valid.
    > >
    > > Xho
    > >

    >
    > Dont really see how that matters as that is another function.


    If you are only interested in throwing the problem over the wall and then
    forgetting about, why ask which way is best? It seems like, at this point,
    it is all the same to you.

    > Maybe Ill
    > delete it.


    Well, then I guess you had better save it to disk first. Or else deleting
    will be pretty hard.

    > Maybe Ill fire a js message, maybe Ill fix it.


    I doubt you will be firing off a js message on the server.

    >
    > But I think a yes or no would be sufficient, a function that returns a 1
    > or 0.


    If that is sufficient, that what is your question?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Mar 2, 2006
    #6
  7. Bryan

    Rick Scott Guest

    (Bryan <> uttered:)
    > What is the best way to validate that an uploaded file (using
    > CGI.pm) is a tab delimited table of the correct format? I.e.
    > every line has the same number of columns as the header, and the
    > header has to match a predefined set of table headers. Should it
    > be done as the file is read in? After I store it to disk?
    >
    > Suggestions for implementations?


    Have you searched CPAN? Does Text::TabFile do most of what you need?




    Rick
    --
    key CF8F8A75 / print C5C1 F87D 5056 D2C0 D5CE D58F 970F 04D1 CF8F 8A75
    ADMIRATION, n. Our polite recognition of another's resemblance to
    ourselves.
    :Ambrose Bierce, "The Devil's Dictionary" (1911)
     
    Rick Scott, Mar 2, 2006
    #7
  8. Gunnar Hjalmarsson wrote:
    > Bryan wrote:
    >> What is the best way to validate that an uploaded file (using CGI.pm) is
    >> a tab delimited table of the correct format? I.e. every line has the
    >> same number of columns as the header, and the header has to match a
    >> predefined set of table headers. Should it be done as the file is read
    >> in? After I store it to disk?

    >
    > I don't understand how you would be able to validate the file before it
    > has been stored to disk, at least temporarily.


    You (or more exactly, the CGI module) are reading the contents of the file
    from stdin. In general, it is certainly possible to validate the contents
    as it is being read - it doesn't have to be stored at all. However, CGI
    stores the contents in a temporary file, so usually, when you get around to
    it, it is already on disk. But from reading the docs, it looks like
    upload_hook might be used to change that (I've never used it).

    hp
    --
    This is not a signature
     
    Peter J. Holzer, Mar 5, 2006
    #8
  9. Bryan

    Guest

    "Peter J. Holzer" <> wrote:
    > Gunnar Hjalmarsson wrote:
    > > Bryan wrote:
    > >> What is the best way to validate that an uploaded file (using CGI.pm)
    > >> is a tab delimited table of the correct format? I.e. every line has
    > >> the same number of columns as the header, and the header has to match
    > >> a predefined set of table headers. Should it be done as the file is
    > >> read in? After I store it to disk?

    > >
    > > I don't understand how you would be able to validate the file before it
    > > has been stored to disk, at least temporarily.

    >
    > You (or more exactly, the CGI module) are reading the contents of the
    > file from stdin. In general, it is certainly possible to validate the
    > contents as it is being read - it doesn't have to be stored at all.
    > However, CGI stores the contents in a temporary file, so usually, when
    > you get around to it, it is already on disk. But from reading the docs,
    > it looks like upload_hook might be used to change that (I've never used
    > it).


    upload_hook lets you peek at the file-data while it is being saved to disk,
    but doesn't let you peek at instead of it being written to disk.

    But it is a fairly simple hack to make it work that way, something like:

    if ($DISABLE_UPLOADS) {
    while (defined($data = $buffer->read)) {
    if (defined $self->{'.upload_hook'}) {
    $totalbytes += length($data);
    &{$self->{'.upload_hook'}}($filename ,$data,
    $totalbytes, $self->{'.upload_data'});
    }
    }
    last UPLOADS;
    }



    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Mar 5, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vicky

    Checking file upload size

    Vicky, Feb 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    372
    =?Utf-8?B?QXZuZWVzaA==?=
    Feb 11, 2004
  2. =?Utf-8?B?U2FyYXY=?=

    Upload a file without file Upload control - ASP.Net

    =?Utf-8?B?U2FyYXY=?=, Aug 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,542
    Bruce Barker
    Aug 3, 2005
  3. Mel
    Replies:
    4
    Views:
    499
    Jan Faerber
    Nov 23, 2004
  4. Randy Kramer
    Replies:
    2
    Views:
    418
    Randy Kramer
    Jan 12, 2007
  5. Wesley
    Replies:
    1
    Views:
    179
    Wesley
    Apr 15, 2014
Loading...

Share This Page