Upload file format checking

B

Bryan

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
 
X

xhoster

Bryan said:
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
 
G

Gunnar Hjalmarsson

Bryan said:
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.
 
B

Bryan

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.
 
A

A. Sinan Unur

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
 
X

xhoster

Bryan said:
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
 
R

Rick Scott

(Bryan said:
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
 
P

Peter J. Holzer

Gunnar said:
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
 
X

xhoster

Peter J. Holzer said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top