Gunnar Hjalmarsson said:
This simple upload script:
http://groups.google.com/[email protected]
works fine on Windows, and can be run with tainting enabled. Since it
makes use of the upload() function instead of param() to grab the
filehandle, it may also be more robust.
I used the script and it uploads the file but I don't want to give the
file the name of the starttime. I want it to be the original filename.
How would I do that?
#!c:/perl/bin/perl
use strict;
use warnings;
use CGI;
my $dir = 'c:/progra~1/apache~1/apache2/htdocs/quickbooks';
my $starttime = time;
my $q = new CGI;
my $fh = $q->upload('upfile');
$q->uploadInfo($fh)->{'Content-Disposition'} =~ /filename="([\w\-\.
]+)"/;
my $name = ($1 or $starttime);
open FILE, "> $dir/$name" or die $!;
binmode FILE;
print FILE $_ while <$fh>;
close FILE;
print "Content-type: text/html\n\n";
print "$name was uploaded.<br>";
print "It took ", time - $starttime,
Also on my other script I get this error exactly.
Cannot chdir to C:\Documents and Settings\Mark\Desktop\Trifold
Slide&Ride.doc:No such file or directory at C:/Program Files/Apache
Group/Apache2/htdocs/uploader.cgi line 22. Now I don't know why it
isn't talking about chdir when I only do that once through the whole
script. I feel like this script is close to finished but one or two
tiny things are wrong. If anybody has apache on Windows could they
test this code out. Thanks.
Now if go to line 22 in notepad it is this my $path = abs_path $file;
here is my script again
#!c:/perl/bin/perl
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use strict;
use warnings;
use Cwd qw/cwd abs_path/;
my $q = CGI->new();
my $file = $q->param('upfile');
$file =~ /([\w.-]+)$/ or die "Unaccetable file name: $file";
my $filename = $1;
my $cdir = cwd;
chdir('C:/Program Files/Apache Group/Apache2/htdocs/quickbooks') or
die "Can't cd to quickbooks dir: $!";
my $dir = cwd;
my $path = abs_path $file;
my $notes = $q->param('notes');
print $q->header, $q->start_html('Uploading File');
print $q->h1('Upload Results');
if(!$file){
print "Nothing Uploaded\n";
} else {
print "Filename: $file<br />\n";
print "Destination: $path<br />\n";
print "Original CWD was: $cdir<br />\n";
print "New CWD after chdir is: $dir<br />\n";
my $ctype = $q->uploadInfo($file)->{'Content-Type'};
print "MIME Type: $ctype<br />\n";
open(OUTFILE, ">$path") or die "Can't create path $!";
binmode(OUTFILE);
my ($read, $buffer);
print OUTFILE $buffer
while $read = read $file, $buffer, 1024;
defined $read or die "read failed: $!";
close OUTFILE or die "Close of uploaded file failed: $!";
close $file or die "Close of socked failed $!";
print "File saved\n";
}
$q->end_html;