#!/usr/bin/perl use strict; use warnings; use CGI; use HTML::Template; use Time::Piece; use CGI::Session; use Data::Dumper; use Net::FTP; # File date string my $date_ntime = localtime->strftime('%Y%m%d%H%M%S'); my $cgi = CGI->new(); # Create new session my $session = new CGI::Session("driver:File", $cgi, {Directory => "sesh"}) or die CGI::Session->errstr;; my $sid = $session->id(); my $tmpl = HTML::Template->new(filename => 'templates/test_up.tmpl', die_on_bad_params => 0, associate => $session); my ($msg_ok, $msg, $file_msg); # Check app access - if not authorized show admins my $user_name = "XYZ"; # This is the value of the name of the file benn uploaded. my $got_doc_name = $cgi->param( 'doc' ) || ''; # Store the file name been uploaded so we can use it to inform the user later. # Add the session parameter in here. Use .= to ensure a string and not a file handle. my $got_file_name .= $cgi->param( 'doc_upload' ) || $session->param( 'doc_uploaded' ) || ''; # Write the session to disk with flush $session->param("doc_uploaded", $got_file_name); $session->flush(); # Process file process_request($got_file_name, $user_name, $date_ntime, $got_doc_name); # Load this value into the template $tmpl->param( USER_NAME => $user_name, ); my $cookie = $cgi->cookie(CGISESSID => $sid); print $cgi->header(-cookie=>$cookie ), $tmpl->output; exit; sub process_request { my ($file_name, $user_name, $date_ntime, $doc_name) = @_; #return unless $file_name; # File selected by the user. my @file_types = ( {filename => "Summary_${date_ntime}_x.csv", ext => 'csv', doc => 'sum'}, {filename => "Results_${date_ntime}_y.txt", ext => 'txt', doc => 'res'}, {filename => "History_${date_ntime}_z.xlsx", ext => 'xlsx', doc => 'his'}, ); # Get the file extension only - normal win style extensions my ($file_ext_uploaded) = $file_name =~ /((\.[^.\s]+)+)$/; $file_ext_uploaded =~ s/^\.//; my ( $file_renamed, $ext ); # Get the number of items (hashes) in the array. my $items = scalar (@file_types); for (my $i=0; $i < $items; $i++) { if ($file_types[$i]{'doc'} eq $doc_name) { $file_renamed = $file_types[$i]{'filename'}; $ext = $file_types[$i]{'ext'}; last; } } warn " Original filename:$file_name | File renamed:$file_renamed^ \n" if $file_renamed; if ( $file_ext_uploaded eq $ext) { $cgi->upload( 'doc_upload' ); my $tmp_file = $cgi->tmpFileName( $file_name ); warn " ^$tmp_file^ to ^$file_renamed^"; rename("$tmp_file", "$file_renamed") || die ( "Error in renaming" ); warn qq{rename ("$tmp_file", "$file_renamed");\n}; # The line above is printing an empty "$tmp_file" why? chmod 0664, "tmp/$file_renamed"; # FTP FILE. my $host = 'xxx'; my $user = 'yyy'; my $pwd = 'zzz'; my $ftp_dir = '/'; my $ftp = Net::FTP->new($host, Debug => 0, Passive => 0) or die "Could not connect to '$host': $@"; $ftp->login($user, $pwd) or die sprintf "Could not login: %s", $ftp->message; $ftp->cwd($ftp_dir) or die sprintf "Could not login: %s", $ftp->message; # Get a list of files in the FTP server my @retrived = $ftp->ls("file_types"); if (@retrived) { warn " File $file_renamed already exists in server."; }else{ warn " *$file_renamed*"; #The error: -> Cannot open Local file Summary_20180115102920_x.csv: No such file or directory my $put_file = $ftp->put("$file_renamed") or die "Cannot put file ", $ftp->message if $file_renamed; warn " FTP transaction was successful for file(s): $put_file"; } $ftp->quit; }else { warn "Wrong file type."; } }