Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to send a file to CGI program ?

         

Emile

1:08 pm on Aug 13, 2001 (gmt 0)

10+ Year Member



Hi !
I try to send a file to a CGI Program an am using the BALISE <INPUT TYPE="file"....> of <FORM> with ENCTYPE="multi-part/form-data"
but it doesn't functionate !!

I'm using an Apache Server on Solaris and want that someone which is on a PC can send a file on his hard drive to the server....When the CGI receives the file, two possibilities:
1)He saves the file on the hard drive of server
2)He reads the file in order to get his differents fields

I found the following example (that I changed a little) on this forum:

#!/usr/bin/perl
use CGI qw(:standard);
$SAVE_DIRECTORY = "/usr/local/apache/cgi-bin/barkatz";

foreach $key (sort{$a <=> $b} $query->param(uploadfile))
{
next if($key !~ /^uploadfile/);

if($query->param($key) =~ /([^\/\\]+)$/)
{
$Filename = $1;
$Filename = ~ s/^\.+//;
$Filename = ~ s/\"//;
$File_Handle = $query->param($key);

if(!open(OUTFILE, ">$SAVE_DIRECTORY/$Filename"))
{
print "Content-type: text/html\n\n";
print "Error:\n<BR>";
print "File:$SAVE_DIRECTORY/$Filename\n<BR>";
print "Filename:$Filename\n<BR>";
exit;
}

undef $BytesRead;
undef $Buffer;
binmode OUTFILE;
while ($Bytes = read($File_Handle, $Buffer, 1024))
{
$BytesRead += $Bytes;
print OUTFILE $Buffer;
$filecontents .= $Buffer;
}

close OUTFILE;

}
}

I'm not sure that's the good example, does it ?
Perhaps is it because it's written in PERL 5 and I only use PERL 4 ?

When I make perl savefile.pl, I obtain the message:
Can't call method param on an undefined value on savefile.pl line 5

If you could give me some indication.
Thanks.

sugarkane

1:23 pm on Aug 13, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, Emile.

First of all, you need to add the line

$query=CGI::new();

somewhere near the start of your script (after the use CGI; but before any use of $query->param)

This basically links the variable $query to the CGI module, allowing you to access all the modules methods through it.

This will stop the error message - see if it works now :)

Emile

3:36 pm on Aug 13, 2001 (gmt 0)

10+ Year Member



Hi!

I follow your indication and put the line $query=CGI::new(); before use CGI
The script presents no error when I make perl savefile.pl but the Apache server gives the following message:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@dmi.ens.fr and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

What does that mean ?
Is it because I'm not root on this machine, so I can import a file from a host ?

Thank you for your help.

Air

12:46 am on Aug 18, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More than likely you don't have the module CGI.pm, which you need if you are going to "use CGI". It has been included as part of the standard perl distribution starting with perl 5. If you have an older version of perl (pre ver5) then either the CGI.pm module will have to be installed or an upgrade to perl 5 is necessary.

You can also use the cgi-lib module to make file uploads easier if you don't have CGI.pm, cgi-lib can be copied into the cgi-bin directory and "include" it in you scripts. It's easy to use.

<added>
I just noticed that you said "I follow your indication and put the line $query=CGI::new(); before use CGI "
Note that it should go after use CGI, not before.
</added>

Emile

3:02 pm on Aug 21, 2001 (gmt 0)

10+ Year Member



Hello,

I change $query=CGI::new(); after use CGI;
But I obtain the same error that before.
It's probably because I haven't the lib cgi.pm

But I obtain the same Internal Server Error,
if I don't use the lib cgi.pm !!
Take a look to the following program which is
inspired of a book:

#!usr/local/bin/perl

use CGI;
$query = new CGI;

$upload_directory = "/usr/local/apache/cgi-bin/emile/";

if($query->param('send_file'))
{
&print_page_start;
&write_file;
&print_page_end;
}

else
{
&print_page_start;
&print_form;
&print_page_end;
}

sub print_form
{
print "<FORM ACTION=\"http://cgi.di.ens.fr/cgi-bin/emile/savefile.pl\" METHOD=\"post\" ";
print "ENCTYPE = \"multipart/form-data\">\n";
print "<INPUT TYPE=\"file\" NAME=\"send_file\"><BR>\n";
print "<INPUT TYPE=\"submit\" VALUE=\"upload file\"><BR>\n";
print "</FORM>\n";
}

sub print_page_start
{
print $query->header;
print "<HTML><HEAD><TITLE>Envoi d'un fichier via un formulaire HTML</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H3>Envoi d'un fichier via un formulaire HTML</H3>\n";
}

sub write_file
{
$filename = $query->param('send_file');
if($filename =~ /.*[\/\\](.*)/)
{
$outfilename = $1;
}

else
{
$outfilename = $filename;
}

print "$filename<BR>\n";
$counter = 0;

while(-e "$upload_directory/$out_filename")
{
$counter++;
$out_filename =~ s/^(.+)\.(.+)$/$1$counter\.$2/;
}
print "$filename<BR>\n";

open (OUTFILE, "> $upload_directory/$out_filename");
while($byteread = read($filename, $buffer, 1024))
{
print OUTFILE $buffer;
}

close $filename;
close OUTFILE;
}

sub print_success
{
print "<P>Fichier enregistré avec succès</P>";
}

sub print_page_end
{
print "</BODY></HTML>\n";
}

I don't understand why it doesn't functionate.
Does it come from the Apache Server ?

Thank you for your help.

;-)