For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > August 2006 > File upload problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author File upload problem
rekhadas@gmail.com

2006-08-09, 6:55 pm

I have to upload a file from our one of our webservers. I have
following html form:

<form name=uploadform enctype="multipart/form-data"
action="upload_file.cgi" method="POST">
<input name="uploadedfile" type="file" />
<input type="submit" value="Upload File" />
</form>

The problem is when I submit the data to upload_file.cgi I have to just
validate the file params and send it to another cgi-script which will
finally upload the file to the server. I don't know how to send this
data from upload_file.cgi to the other cgi script.

I am trying this:
use CGI;

my $query = new CGI;
my $request = POST $url, Content_Type => 'multipart/form-data', Content
=> $query->param;

but this is giving me "Malformed multipart POST" error. Can someone
tell me how to do it?

thanks.

Kenneth Tomiak

2006-08-11, 6:55 pm

You don't have to use another script to finally upload the file. You chose
to make your own life more complicated. Lots of people let the script that
gets the file from the web page upload the file. Are you trying to send the
file back out to a web page so you can get it again. Seems silly to me. If
this other script is not part of the cgi processing then let the script that
got the file write it to a file and pass the file name to the next script.




<rekhadas@gmail.com> wrote in message
news:1155158412.946026.326060@m73g2000cwd.googlegroups.com...
>I have to upload a file from our one of our webservers. I have
> following html form:
>
> <form name=uploadform enctype="multipart/form-data"
> action="upload_file.cgi" method="POST">
> <input name="uploadedfile" type="file" />
> <input type="submit" value="Upload File" />
> </form>
>
> The problem is when I submit the data to upload_file.cgi I have to just
> validate the file params and send it to another cgi-script which will
> finally upload the file to the server. I don't know how to send this
> data from upload_file.cgi to the other cgi script.
>
> I am trying this:
> use CGI;
>
> my $query = new CGI;
> my $request = POST $url, Content_Type => 'multipart/form-data', Content
> => $query->param;
>
> but this is giving me "Malformed multipart POST" error. Can someone
> tell me how to do it?
>
> thanks.
>



Squalle

2006-08-18, 9:55 pm

I'm trying to do something similar. I have a form to upload an image. In the
form, is two inputs, "category" and "file". I'd like the file to be uploaded
to the folder defined by the category select box. For example: in the form,
when I input the category "stuff" and then select the file "junk.jpg", I'd
like the image uploaded to www.mysite.com/subfolder/stuff/junk.jpg

I have the upload form that works fine by itself. When I try to incorporate
the "category", it says the upload worked, but the image doesn't get
uploaded.

Thank you.
Rick


<rekhadas@gmail.com> wrote in message
news:1155158412.946026.326060@m73g2000cwd.googlegroups.com...
>I have to upload a file from our one of our webservers. I have
> following html form:
>
> <form name=uploadform enctype="multipart/form-data"
> action="upload_file.cgi" method="POST">
> <input name="uploadedfile" type="file" />
> <input type="submit" value="Upload File" />
> </form>
>
> The problem is when I submit the data to upload_file.cgi I have to just
> validate the file params and send it to another cgi-script which will
> finally upload the file to the server. I don't know how to send this
> data from upload_file.cgi to the other cgi script.
>
> I am trying this:
> use CGI;
>
> my $query = new CGI;
> my $request = POST $url, Content_Type => 'multipart/form-data', Content
> => $query->param;
>
> but this is giving me "Malformed multipart POST" error. Can someone
> tell me how to do it?
>
> thanks.
>



Kenneth Tomiak

2006-08-20, 6:55 pm

Add the ${category} field to the path before the ${file} field. Make sure
you use the correct slash in your code. I usually have two back slashes when
enclosed in quotes. You might be able to use one forward slash, but that
depends on the operating system and how you code things.

Of course if you showed the portion of code where you do this you might get
the help you want. And exactly how is your attempt to load one file similar
to doing two? That it is an upload? I'm sure if you did a real search you'll
find plenty of samples of how to do this.


"Squalle" <squalle@friendlynet.com> wrote in message
news:FPuFg.77582$Eh1.25229@tornado.ohiordc.rr.com...
> I'm trying to do something similar. I have a form to upload an image. In
> the form, is two inputs, "category" and "file". I'd like the file to be
> uploaded to the folder defined by the category select box. For example: in
> the form, when I input the category "stuff" and then select the file
> "junk.jpg", I'd like the image uploaded to
> www.mysite.com/subfolder/stuff/junk.jpg
>
> I have the upload form that works fine by itself. When I try to
> incorporate the "category", it says the upload worked, but the image
> doesn't get uploaded.
>
> Thank you.
> Rick
>
>
> <rekhadas@gmail.com> wrote in message
> news:1155158412.946026.326060@m73g2000cwd.googlegroups.com...
>
>



Squalle

2006-08-20, 9:55 pm

First off, thanks for the response.

Here is the first part of the script:

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/~!/ ~!/g; $FORM{$name} = $value;}

$category = $FORM{'category'}; ## get the category from the form
$upic = "/home/sites/site2/web/funnypics/$category"; ## set the path to the
selected category
@good_extensions = ('gif', 'jpg');
@bad_extensions = ();
$redirect = "";
$max_size = 1000;
$max_num_files = 1;

Like before, the script says it worked, but the file isn't there, or
anywhere for that matter. Is there something I'm missing?

Thanks!



"Kenneth Tomiak" <kennethtomiak@optonline.net> wrote in message
news:6I%Fg.409$%D.11@newsfe12.lga...
> Add the ${category} field to the path before the ${file} field. Make sure
> you use the correct slash in your code. I usually have two back slashes
> when enclosed in quotes. You might be able to use one forward slash, but
> that depends on the operating system and how you code things.
>
> Of course if you showed the portion of code where you do this you might
> get the help you want. And exactly how is your attempt to load one file
> similar to doing two? That it is an upload? I'm sure if you did a real
> search you'll find plenty of samples of how to do this.
>
>
> "Squalle" <squalle@friendlynet.com> wrote in message
> news:FPuFg.77582$Eh1.25229@tornado.ohiordc.rr.com...
>
>



Kenneth Tomiak

2006-08-21, 6:55 pm

You don't show where ${upic} gets used with the ${file} name. So, make sure
when you join ${upic} and ${file} you have the '/' between them.

${pathandfile] = "${upic}/${file}" ;

Otherwise the filename will start with the category as part of te filename.

These category subdirectories must already exist and your webserver must
have write authority.




"Squalle" <squalle@friendlynet.com> wrote in message
news:ke9Gg.78211$Eh1.70808@tornado.ohiordc.rr.com...
> First off, thanks for the response.
>
> Here is the first part of the script:
>
> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
> @pairs = split(/&/, $buffer);
> foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair);
> $value =~ tr/+/ /;
> $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> $value =~ s/~!/ ~!/g; $FORM{$name} = $value;}
>
> $category = $FORM{'category'}; ## get the category from the form
> $upic = "/home/sites/site2/web/funnypics/$category"; ## set the path to
> the selected category
> @good_extensions = ('gif', 'jpg');
> @bad_extensions = ();
> $redirect = "";
> $max_size = 1000;
> $max_num_files = 1;
>
> Like before, the script says it worked, but the file isn't there, or
> anywhere for that matter. Is there something I'm missing?
>
> Thanks!
>
>
>
> "Kenneth Tomiak" <kennethtomiak@optonline.net> wrote in message
> news:6I%Fg.409$%D.11@newsfe12.lga...
>
>



Squalle

2006-08-22, 3:55 am

Well, again thanks for trying...

I tried what you suggested, but to no avail. I have found a workaround,
however it's not that great.

What I ended up doing (for now anyway) is using the script to call one of
many other scripts with the category names. 12 categories = 12 more scripts.
The category scripts are not that large, and speed is not an issue.
Basically, the form with send the data to the main script, which will show a
second form with the file upload input. Then, depending on which category is
chosen in the main form, the main script will call that category's script,
which will upload the file to the correct folder.

I realize this may seem like a goofy way to do this, but it's working for
now. Thanks for your time!

Rick


"Kenneth Tomiak" <kennethtomiak@optonline.net> wrote in message
news:IWoGg.32$T11.0@newsfe08.lga...
> You don't show where ${upic} gets used with the ${file} name. So, make
> sure when you join ${upic} and ${file} you have the '/' between them.
>
> ${pathandfile] = "${upic}/${file}" ;
>
> Otherwise the filename will start with the category as part of te
> filename.
>
> These category subdirectories must already exist and your webserver must
> have write authority.
>
>
>
>
> "Squalle" <squalle@friendlynet.com> wrote in message
> news:ke9Gg.78211$Eh1.70808@tornado.ohiordc.rr.com...
>
>



alpha_beta_release

2006-08-28, 3:55 am

Try use modules CGI and CGI::UploadEasy from CPAN. Easier

Squalle wrote:[color=darkred]
> Well, again thanks for trying...
>
> I tried what you suggested, but to no avail. I have found a workaround,
> however it's not that great.
>
> What I ended up doing (for now anyway) is using the script to call one of
> many other scripts with the category names. 12 categories = 12 more scripts.
> The category scripts are not that large, and speed is not an issue.
> Basically, the form with send the data to the main script, which will show a
> second form with the file upload input. Then, depending on which category is
> chosen in the main form, the main script will call that category's script,
> which will upload the file to the correct folder.
>
> I realize this may seem like a goofy way to do this, but it's working for
> now. Thanks for your time!
>
> Rick
>
>
> "Kenneth Tomiak" <kennethtomiak@optonline.net> wrote in message
> news:IWoGg.32$T11.0@newsfe08.lga...

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com