Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Storing a file from a cgi parameter

1 view
Skip to first unread message

J Alejandro Ceballos Z

unread,
Jun 16, 2009, 10:29:47 PM6/16/09
to beginn...@perl.org

I am trying to store a file in the temp directory.

It creates the desired file, but with size 0.

The files are videos sent via CGI. I tried with the upload function,
but it did not work too.

Would you please give me some direction about what I am doing wrong or
what should I do in order to make it work?

Thank you.

# Load system modules (strict disabled in order to prevent conflict)
use CGI;
# use strict;

# Create CGI
my $cgi_this = new CGI;
my $file_file = $cgi_this->param('sbvideo_file') || 0;

# retrieve original name and change it
if ($file_file =~ /(.+)\.(\w+)$/)
{ ($str_filename,$str_fileext) = ($1,$2); }
$str_filename = $$ . '.'. $str_fileext;

# this line does not work, sends error on hash content type key
# $str_filetype = $cgi_this->uploadInfo($file_file)->{'Content-Type'};

# store file (that does not work)
if (open (hFILE, ">~/tmp/$str_filename"))
{
binmode hFILE;
while (<$file_file>)
{ print hFILE $_; }
close (hFILE);
}

# store file 2nd option (and this one neither)
if (open (hFILE, ">~/tmp/$str_filename"))
{
binmode hFILE;
while (read($file_file,$str_buffer,1024))
{ print hFILE $str_buffer; }
close (hFILE);
}


Atentamente,

J. Alejandro Ceballos Z.

w: http://alejandro.ceballos.info
e: bu...@alejandro.ceballos.info
m: +52 (33) 1411.6079

Gunnar Hjalmarsson

unread,
Jun 17, 2009, 12:46:44 PM6/17/09
to beginn...@perl.org
J Alejandro Ceballos Z wrote:
>
> I am trying to store a file in the temp directory.
>
> It creates the desired file, but with size 0.
>
> The files are videos sent via CGI. I tried with the upload function, but
> it did not work too.
>
> Would you please give me some direction about what I am doing wrong or
> what should I do in order to make it work?

I suggest that you try the CGI::UploadEasy module.

http://search.cpan.org/dist/CGI-UploadEasy/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Alexander Krasnorutsky

unread,
Jun 19, 2009, 3:25:14 PM6/19/09
to beginn...@perl.org

You can try use the "$cgi_this->upload" method to retrieve the
filehandle. There is a standard module named "File::Temp" for generating
names for temporary files.

--
Alexander Krasnorutsky.
Email: krasn...@mail.ru.

J Alejandro Ceballos Z

unread,
Jun 21, 2009, 11:51:10 PM6/21/09
to beginn...@perl.org

As I understand, the code must be overwritten like:

#!/usr/bin/perl -T
use CGI::UploadEasy;
use CGI;
use strict;
use warnings;

# create and load data
my $ue = CGI::UploadEasy->new(-uploaddir => '~/tmp');


my $file_file = $cgi_this->param('sbvideo_file') || 0;

# retrieve original name and change it
if ($file_file =~ /(.+)\.(\w+)$/)
{ ($str_filename,$str_fileext) = ($1,$2); }
$str_filename = $$ . '.'. $str_fileext;

# STORE???
# is this line Ok? To assign directly the file to that address?
$ue->fileinfo = $file_file;
#Now. How I change the name of the stored file to the new one of
$str_filename

I need to validate some data from the parameter, and change other,
like the name of the file, that is why I use CGI and CGI::UploadEasy,

There is some example? Not only the CPAN documentation in order to
check it?

J Alejandro Ceballos Z wrote:
> I am trying to store a file in the temp directory.
> It creates the desired file, but with size 0.
> The files are videos sent via CGI. I tried with the upload function,
> but it did not work too.
> Would you please give me some direction about what I am doing wrong
> or what should I do in order to make it work?

I suggest that you try the CGI::UploadEasy module.

http://search.cpan.org/dist/CGI-UploadEasy/


Gunnar Hjalmarsson

unread,
Jun 22, 2009, 12:02:57 PM6/22/09
to beginn...@perl.org
J Alejandro Ceballos Z wrote:
> Gunnar Hjalmarsson wrote:
>> J Alejandro Ceballos Z wrote:
>>> I am trying to store a file in the temp directory.
>>>
>>> It creates the desired file, but with size 0.
>>>
>>> The files are videos sent via CGI. I tried with the upload function,
>>> but it did not work too.
>>>
>>> Would you please give me some direction about what I am doing wrong or
>>> what should I do in order to make it work?
>>
>> I suggest that you try the CGI::UploadEasy module.
>>
>> http://search.cpan.org/dist/CGI-UploadEasy/
>
> As I understand, the code must be overwritten like:
>
> #!/usr/bin/perl -T
> use CGI::UploadEasy;
> use CGI;

That line is redundant, since CGI::UploadEasy loads CGI.pm.

> use strict;
> use warnings;
>
> # create and load data
> my $ue = CGI::UploadEasy->new(-uploaddir => '~/tmp');

At this point, you ought to have the file in your tmp directory with
*basically* its original name. A safe way to find out the exact name
it's saved as would be:

my $file_file;
my $info = $ue->fileinfo;
for my $file ( keys %$info ) {
if ( $info->{$file}{ctrlname} eq 'sbvideo_file' ) {
$file_file = $file;
last;
}
}

> my $file_file = $cgi_this->param('sbvideo_file') || 0;

You did not create a $cgi_this object. OTOH, grabbing the file name from
param() is not safe, since CGI::UploadEasy may have altered the name
slightly.

> # retrieve original name and change it

Missing declarations:

my ($str_filename, $str_fileext);

> if ($file_file =~ /(.+)\.(\w+)$/)
> { ($str_filename,$str_fileext) = ($1,$2); }
> $str_filename = $$ . '.'. $str_fileext;
>
> # STORE???
> # is this line Ok? To assign directly the file to that address?
> $ue->fileinfo = $file_file;

No. That would replace the fileinfo object with just the file name.
Doing so makes no sense to me.

> #Now. How I change the name of the stored file to the new one of
> $str_filename

Use the rename() function. http://perldoc.perl.org/functions/rename.html

> I need to validate some data from the parameter, and change other, like
> the name of the file, that is why I use CGI and CGI::UploadEasy,
>
> There is some example? Not only the CPAN documentation in order to check
> it?

Well, as the docs say, the names of the other parameters besides the
filename can be grabbed via $ue->otherparam, and CGI.pm's param() is the
way to get the values.

HTH

0 new messages