Click to See Complete Forum and Search --> : Help with uploading a file


cdavis1986
11-26-2008, 09:36 AM
Hello! I am trying to upload a file using a perl script. When I submit the file, everything on the next page loads, the only problem is that the file isn't located in the upload directory! I'm not sure why it isn't working..can anyone give me a hand? Thanks!


-------------Code-------------------
#!c:/perl/bin -w

#use strict;
use warnings;
use CGI qw(:standard);
use Win32::OLE;
use Mail::Sendmail;
use Win32::OLE::Const 'Microsoft ActiveX Data Objects';
use Win32::OLE::Variant;
use Win32::ODBC;
use HTML::Entities;
use String::Util ':all';

#custom modules
use DxMod::GetTime;

#getting the post array so that
#it is printable
#such as: $Form{field}
my %Form;
foreach my $field (param()){
$Form{$field} = param($field);
$Form{$field} = crunch($Form{$field});
$Form{$field} = unquote($Form{$field});
$Form{$field} = define($Form{$field});
$Form{$field} =~ s/\'//g;
$Form{$field} =~ s/\"//g;
}



#This is the
#subroutine
# SEND MAIL
sub mail{

my $message = "$Form{vdesc}";

unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'mail.dxa.com';
%mail = ( To => 'xxx@xx.com',
From => "$Form{vuser}\@dxa.com",
Subject => "Support!",
Message => $message
);
sendmail(%mail) or die $Mail::Sendmail::error;
}#sub mail

&mail();

###########
#Handle the file upload
###########

$CGI::POST_MAX = 30048;

my $cgi = new CGI;
my $dir = "cdavissandbox/upload";
my $file = $cgi->param('file');

if ($file)
{
$file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename
$name = $2;
open(LOCAL, ">$dir/$name") or die $!;
binmode LOCAL;
while(<$file>) {
print LOCAL $_;
}
close LOCAL;

$message = "Uploaded file: '$dir/$name'<br/>";
}
###########
##Done with file#
###########

#getting the 2 dates for the DB
my $theTime = TimeDate();
my $subdate = TimeDate("date");

#My database code
my $sql = "INSERT into support_calls (ID, vdate, vdesc, vuser) VALUES ('$theTime', '$subdate', '$Form{vdesc}', '$Form{vuser}')";
if ($file)
{$sql = "INSERT into support_calls (ID, vdate, vdesc, vuser, vfile)
VALUES ('$theTime', '$subdate', '$Form{vdesc}', '$Form{vuser}', '$dir/$name')";}

my $db = Win32::ODBC->new('support');
$db->Sql($sql);
$db->Close();

print'
<html>
<head>
<title>Support!</title>
</head>
<body>
<div style="border:2px;border-style:dashed;width:50%;margin:10px;padding:15px;">
Thank you for submitting a request to Support! We will take care of it
as soon as we are able. Please check your E-mail periodically because we may need more information about
the issue. Thanks!<br/><br/>
';
if ($message){print $message;}
print'
<a href="javascript:history.go(-1)">[Submit Another Request]</a>
<a href="">[Go Home]</a>
</div>
</body>
</html>';

cdavis1986
11-26-2008, 11:00 AM
hmmm. Nobody has any ideas?

Sixtease
11-26-2008, 02:20 PM
while(<$file>)
If I see correctly, $file is the file name. You need to open it first.
open my $filehandle, $file;
while (<$filehandle>)

But better still, just link or copy the file instead of cat'ing it.
use File::Copy;
link($file, "$dir/$name") or copy($file, "$dir/$name") or die $!

hmmm. Nobody has any ideas?
Patience friend. :-)

cdavis1986
11-26-2008, 02:23 PM
Thanks! You helped me a ton. Yeah...patience is a virtue alright.

Thanks again! I can't believe I missed that..