[Cedarvalley] upload problem
Stephen D. Wells
wells at cedarnet.org
Fri Dec 19 12:51:00 CST 2003
try this:
----
<form action="upload.cgi" method="post" enctype="multipart/form-data">
upload file: <input type="file" name="upload_file"><br>
<input type="submit" value="upload">
</form>
----
upload.cgi
----
#!/usr/bin/perl -w
use strict;
use CGI;
my $q = CGI->new;
my $ret = upload($q, 'upload_file',
{ upload_path => './uploads', overwrite => 1 });
print "Content-type: text/html\n\n";
# error?
if ($ret->{err}) {
print $ret->{err};
exit;
}
# successful
print<<" EOS";
<p><b>$$ret{filename}</b> of size <b>$$ret{length} bytes</b>
was uploaded.</p>
EOS
exit 1;
###########
# input: query, file upload entry name, Attributes
# current attributes: upload_path => 'path that files are to be
uploaded to
# overwrite => 1 or 0 to replace a file or not
# output: hash reference of...
# filename => the final filename that was used for the upload
# length => the size (in bytes) of the uploaded file
sub upload {
my ($q, $file, $attrib) = @_;
# init
$attrib->{overwrite} ||= 0;
$attrib->{upload_path} ||= '.'; # current directory
my $output = {};
my ($filename) = ($q->param($file) =~ /.*[\/\\](.*)$/);
my $FH = $q->upload($file);
# if the file exists and we aren't allowed to overwrite it...
return {err => 'file exists' }
if (-e "$$attrib{upload_path}/$filename" &&
!$attrib->{overwrite});
# upload the file
$output->{filename} = $filename;
open (OUTFILE,">$$attrib{upload_path}/$filename") ||
return {err => "can't create: $$attrib{upload_path}/$filename:
$!"};
# should you lock the file here?
my $buffer;
binmode(OUTFILE);
while (read($FH,$buffer,1024)) {
print OUTFILE $buffer;
$output->{length} += length($buffer);
}
close (OUTFILE);
$output
}
------
On Fri, 2003-12-19 at 09:42, Aaron Thompson wrote:
> The problem seems to be that it doesn't work with Mozilla 1.5 or IE 6 but works
> fine with NS 4.7 (I don't have a copy of a new NS browser to test with).
>
> Here is the code/html:
>
> Thanks for your help.
>
>
> -----<perl>-----
> ...
> sub upload_file{
> my $overwrite = "1"; #overwrite files on upload (yes = 1, no = 0)
> my $length = 0;
> my $file = $query->param("upload-me");
> my $local_file_name = "";
> my $uploadFH;
> my $save_flag;
>
> $file =~ s/.*[\/\\](.*)/$1/;
> $local_file_name = $tmp_dir . "/" .$file;
> print "<p>File Upload...<br>This may take some time, please wait...</p>";
> print "<p>Attempting to upload: $file</p>";
> $save_flag = 1;
> if(! $overwrite){if( -e $local_file_name ){$save_flag = 0;}}#fi
> if($save_flag){
> $uploadFH = $query->upload("upload-me");
> open UPLOADFILE, ">$local_file_name" or
> html_die("Could not open $local_file_name for writing!");
> binmode UPLOADFILE;
> while ( <$uploadFH> ){
> print UPLOADFILE;
> $length += length($_);
> }#elihw
> close UPLOADFILE;
> print "<p><b>$file</b> of size <b>$length bytes</b> was uploaded.</p>";
> }
> else{
> print "<p><b>$file</b> already exists... could not upload.</p>";
> }#fi
> }
> ...
> -----<perl>-----
>
> -----<HTML>-----
> <html>
> <head>
> <title>server.domain</title>
>
> <script language="javascript">
>
> function sure(){
> msg="Are you sure you want upload this file?";
> return (confirm(msg));
> }//erus
> </script>
>
> </head>
> <body>
> <table width="50%" border="1" align="center">
> <tr>
> <td align="center"><b>Upload a gallery for aaron</b></td>
> </tr>
> <tr><td></td></tr>
> <tr><td></td></tr>
> <tr>
> <td>
> <b>NOTE:</b> The following limitations will apply when using this tool...
> <ul>
> <li>this will only work with .zip files</li>
> <li>you should not have any spaces in your image files or zip file.</li>
> <li>Once you agree to send the data nothing will display untill the transfer is complete.</li>
> </ul>
> <br>
> <form action="http://server.domain/admin/index.cgi" methd="post" ENCTYPE="multipart/form-data">
> <input type="hidden" name="option" value="add-galleries">
> <input type="hidden" name="function" value="upload-gallery">
> <input type="hidden" name="gallery" value="test_gallery">
> <table border="0" width="95%" align="center">
> <tr>
> <td align="right">upload file:</td>
> <td><input type="file" name="upload-me"></td>
> </tr>
> </table>
> <div align="center">
> <input type="submit" name="submit" value="submit" onclick="return sure();">
> </div>
> </form>
> </td>
> </tr>
> <tr><td></td></tr>
> <tr><td></td></tr>
> <tr>
> <td align="center">
> <a href="http://server.domain/admin/index.cgi?option=edit-galleries">Edit Galleries</a>
> </td>
> </tr>
> </table>
> </body>
> </html>
> -----<HTML>-----
>
>
>
> On Wed, Dec 17, 2003 at 04:08:58PM -0600, Stephen D. Wells wrote:
> > If I understand you right it works with netscape but not mozilla... Do
> > you have a small complete example (including your html) somewhere?
> >
> > STEVE
> >
> > On Wed, 2003-12-17 at 11:15, Aaron Thompson wrote:
> > > oh yes... I'm not quite sure what I'm doing wrong.
> > >
> > > @
> > >
> > > On Wed, Dec 17, 2003 at 11:10:27AM -0600, Stephen D. Wells wrote:
> > > > I'm back home now and can email again... for some reason the web-based
> > > > mailer I was using wasn't sending out - I'm back to evolution now though
> > > > and things are back to normal. Is this still an issue?
> > > >
> > > > STEVE
> > > >
> > > > On Thu, 2003-12-11 at 13:52, Aaron Thompson wrote:
> > > > > I have been looking and found that I get different data when
> > > > > using mozilla 1.5 or Netscape 4... it seems like I don't get
> > > > > everything... well... back to reading.
> > > > >
> > > > > Netscape:
> > > > >
> > > > > $VAR1 = bless( {
> > > > > '.charset' => 'ISO-8859-1',
> > > > > 'upload-me' => [
> > > > > bless( \*{'Fh::fh00001new_gallery.zip'}, 'Fh' )
> > > > > ],
> > > > > 'submit' => [
> > > > > 'submit'
> > > > > ],
> > > > > 'option' => [
> > > > > 'add-galleries'
> > > > > ],
> > > > > 'gallery' => [
> > > > > 'New_Gallery'
> > > > > ],
> > > > > 'function' => [
> > > > > 'upload-gallery'
> > > > > ],
> > > > > '.tmpfiles' => {
> > > > > '6' => {
> > > > > 'info' => {
> > > > > 'Content-Disposition' => 'form-data; name="upload-me"; filename="new_gallery.zip"',
> > > > > 'Content-Type' => 'application/x-zip-compressed'
> > > > > },
> > > > > 'name' => bless( do{\(my $o = '/var/tmp/CGItemp443')}, 'TempFile' )
> > > > > }
> > > > > },
> > > > > '.parameters' => [
> > > > > 'option',
> > > > > 'function',
> > > > > 'gallery',
> > > > > 'upload-me',
> > > > > 'submit'
> > > > > ],
> > > > > '.fieldnames' => {}
> > > > > }, 'CGI' );
> > > > >
> > > > > Mozilla 1.5:
> > > > >
> > > > > $VAR1 = bless( {
> > > > > '.charset' => 'ISO-8859-1',
> > > > > 'upload-me' => [
> > > > > 'new_gallery.zip'
> > > > > ],
> > > > > 'submit' => [
> > > > > 'submit'
> > > > > ],
> > > > > 'option' => [
> > > > > 'add-galleries'
> > > > > ],
> > > > > 'gallery' => [
> > > > > 'New_Gallery'
> > > > > ],
> > > > > 'function' => [
> > > > > 'upload-gallery'
> > > > > ],
> > > > > '.parameters' => [
> > > > > 'option',
> > > > > 'function',
> > > > > 'gallery',
> > > > > 'upload-me',
> > > > > 'submit'
> > > > > ],
> > > > > '.fieldnames' => {}
> > > > > }, 'CGI' );
> > > > >
> > > > >
> > > > > On Thu, Dec 11, 2003 at 10:54:13AM -0600, Aaron Thompson wrote:
> > > > > > I'm using CGI to upload a file... the file box is 'upload-me' and the code
> > > > > > below works for Netscape 4, but not Mozilla 1.5 or IE 6. Anyone have
> > > > > > experience with this and can point me in the right direction?
> > > > > >
> > > > > > Error message (from apache error log):
> > > > > > Use of uninitialized value in <HANDLE> at /home/httpd/photos/admin/index.cgi line 59.
> > > > > > readline() on unopened filehandle at /home/httpd/photos/admin/index.cgi line 59.
> > > > > >
> > > > > >
> > > > > > the offending code:
> > > > > > ---<snip>---
> > > > > > use CGI;
> > > > > > ...
> > > > > >
> > > > > > $query = new CGI:
> > > > > > ...
> > > > > >
> > > > > > ######################################################################
> > > > > > sub upload_file{
> > > > > > my $overwrite = "0"; #overwrite files on upload (yes = 1, no = 0)
> > > > > > my %valid_mime = ("zip" => "1");
> > > > > > my $length = 0;
> > > > > > my $file = $query->param("upload-me");
> > > > > > my $local_file_name = "";
> > > > > > my $uploadFH;
> > > > > > my $mime;
> > > > > > my $save_flag;
> > > > > >
> > > > > > $file =~ s/.*[\/\\](.*)/$1/;
> > > > > > if($file =~ /.*[\.](.*)/){$mime = $1;}#fi
> > > > > > $local_file_name = $tmp_dir . "/" .$file;
> > > > > > print "<p>File Upload...<br>This may take some time, please wait...</p>";
> > > > > > print "<p>Attempting to upload: $file</p>";
> > > > > > if ($valid_mime{$mime}){
> > > > > > $save_flag = 1;
> > > > > > if(! $overwrite){if( -e $local_file_name ){$save_flag = 0;}}#fi
> > > > > > if($save_flag){
> > > > > > $uploadFH = $query->upload("upload-me");
> > > > > > open UPLOADFILE, ">$local_file_name" or
> > > > > > html_die("Could not open $local_file_name for writing!");
> > > > > > binmode UPLOADFILE;
> > > > > > while ( <$uploadFH> ){
> > > > > > print UPLOADFILE;
> > > > > > $length += length($_);
> > > > > > }#elihw
> > > > > > close UPLOADFILE;
> > > > > > print "<p><b>$file</b> of size <b>$length bytes</b> was uploaded.</p>";
> > > > > > }else{
> > > > > > print "<p><b>$file</b> already exists on the server - not uploaded. </p>";
> > > > > > }#fi
> > > > > > }
> > > > > > else{
> > > > > > print "<p>File not saved... unallowed file time: $mime</p>";
> > > > > > }#fi
> > > > > > }
> > > > > >
> > > > > > ---<snip>---
> > > > > >
> > > > > > Thanks for your help.
> > > > > >
> > > > > > @
> > > > > >
> > > > > >
> > > > > > --
> > > > > > Aaron Thompson Unix Systems Administrator, College of Natural Science
> > > > > > University of Northern Iowa Cedar Falls, IA 50614
> > > > > >
> > > > > > "Because as we know, there are known knowns, there are things we know we
> > > > > > know. We also know there are known unknowns; that is to say we know there are
> > > > > > some things we do not know. But there are also unknown unknowns - the ones we
> > > > > > don't know we don't know." - Donald Rumsfeld
> > > > > > _______________________________________________
> > > > > > Cedarvalley mailing list
> > > > > > Cedarvalley at mail.pm.org
> > > > > > http://mail.pm.org/mailman/listinfo/cedarvalley
> > > > --
> > > > Stephen D. Wells <wells at cedarnet.org>
> > --
> > Stephen D. Wells <wells at cedarnet.org>
--
Stephen D. Wells <wells at cedarnet.org>
More information about the Cedarvalley
mailing list