www.webdeveloper.com
Results 1 to 4 of 4

Thread: Help with uploading a file

  1. #1
    Join Date
    Sep 2008
    Posts
    9

    Help with uploading a file

    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>';

  2. #2
    Join Date
    Sep 2008
    Posts
    9
    hmmm. Nobody has any ideas?

  3. #3
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    392
    while(<$file>)
    If I see correctly, $file is the file name. You need to open it first.
    Code:
    open my $filehandle, $file;
    while (<$filehandle>)
    But better still, just link or copy the file instead of cat'ing it.
    Code:
    use File::Copy;
    link($file, "$dir/$name") or copy($file, "$dir/$name") or die $!
    hmmm. Nobody has any ideas?
    Patience friend. :-)

  4. #4
    Join Date
    Sep 2008
    Posts
    9

    Aw man!

    Thanks! You helped me a ton. Yeah...patience is a virtue alright.

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles