Home  |  Linux  | Mysql  | PHP  | XML
From:Gunnar Hjalmarsson Date:Mon Jun 22 12:02:57 2009
Subject:Re: Storing a file from a cgi parameter
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

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Navigate in group perl.beginners.cgi at sever nntp.perl.org
Previous Next


Your recent visits
problem with PEAR , windows platform , php 5.2.9-2 , IIS Version 6
Re: [PHP-DB] mysqli error
redhatlinux.ahmedyo@gmail.com
Re: [PHP-WIN] Re: [PHP-WEBMASTER] PHP Update & PHP Upgrade
Re: [PHP] 64 Bit IIS 6 ( 32 Bit mode ) + 32Bit php connect with MS-SQL Server


  
© No Copyright
You are free to use Anything
Site Maintained by Zareef Ahmed
Powered By PHP Consultants