http://www.perlmonks.org?node_id=569336

cool has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks!

I am trying to upload a file from a user. Application is written in CGI-Perl. File name is been copied but not the data. I think the problem is with the line  while(read($fh, $buffer, BUFFER_SIZE))  #Write content to OUTPUT file

Also I am not able to upload (application creats the file in my server machine) the file other than /tmp location. I have tried uploading the file in ~/usr1/temp with the permission of temp as 775 and user and group owner as nobody. But its not even able to copy the name of the file as it is doin in /tmp case.

Pl guide me through.

HTML file and the example file that is to be uploaded by user is also attached.

Thank you for your time for reading it.

#!/usr/bin/perl -w use strict; use CGI; use Fcntl qw(:DEFAULT :flock); #SUB error needs to be completed print "Content-type:text/html\n\n"; #Declaring constants use constant UPLOAD_DIR => "/tmp/"; use constant MAX_FILE_SIZE => 1_048_576; #Limit each upload to 1MB use constant MAX_OPEN_TRIES => 100; use constant BUFFER_SIZE => 16_384; # New object $CGI::POSTMAX = MAX_FILE_SIZE; my $query = new CGI; $query->cgi_error and error($query, "Error transferring file: " . $que +ry->cgi_error ); my $file = $query->param('fname')|| error ($query, "No file rec +eived."); my $fh = $query->upload("$file"); my $buffer = ""; my $filename = ""; my @annotation = ""; my @list = ""; $query->header(); $query->start_html(); $query->startform(); ################# IF file has been uploaded ########################## +### if($file) { $file =~ s/[^\w.-]/_/g; #Rectifying the string for name of the fil +e if($file=~ /^(\w[\w.-]*)/) { $file = $1; } else { error($query, "Invalid file name; file must start with a letter or n +umber."); } until (sysopen OUTPUT, UPLOAD_DIR . $file,O_CREAT|O_EXCL ) { $file =~ s/(\d*)(\.\w+)$/($1||0)+1 .$2/e; $1 >= MAX_OPEN_TRIES and error ($query, "Unable to save your file"); } while(read($fh, $buffer, BUFFER_SIZE)) #Write content to OUTPUT file { print OUTPUT $buffer; } close OUTPUT; # $filename = $file.'.'.'fasta'; $filename = $file; # if($format eq "embl") # { #embl_fasta(); # } # elsif($format eq "genbank") # { #genbank_fasta(); # } open(IN,"UPLOAD_DIR/$filename")||die error($query, "cant open from UPL +OAD dir"); my $one = 0; my $header; my $prevhead; my $sequence; #reading the genome sequence my $x=1; while(<IN>) { my $seqline = $_; $seqline =~ s/\n//g; if($seqline =~ /^>/g) { if($one == 0) { $header = $seqline; $prevhead = $header; $sequence = ''; $one++; next; } else { $header = $seqline; $annotation[$x]=$prevhead; $list[$x]=$sequence; $x++; $prevhead = $header; $sequence = ''; next; } }#end if $seqline $sequence = $sequence.$seqline; $sequence =~ s/\s//g; }#end while<IN1> close(IN); $annotation[$x]=$prevhead; $list[$x]=$sequence; } # If Sequence has been uploaded ################# Opening of each sequence ########################### +#### my $z=0; for my $name(@annotation) { my $sequence=""; $sequence = $list[$z]; print "$name\t$sequence\n"; $z++; print $query->p("$sequence"); print "<B> <center><h2>PROMOTER PREDICTION AT A SENSITIVITY LEVEL </h2 +></center><br><br>"; print " <body bgcolor=\"#E0E0E0 \">"; print "<B><em><font color=\"#800000\"> Sequence ID :</em></font> &nbsp +;&nbsp</B>$sequence<br>"; print "<br><BLINK><font color=\"#800000 \">RESULTS</font><br><br> </BL +INK></B>"; print "<hr>"; print "<h3><font color=\"#800000\">FORWARD STRAND</font></h3><br>"; print "<hr>"; } $query->endform(); $query->end_html(); sub error { my ($q, $reason)=@_; print $q -> header ("text/html"), $q -> start_html ("Error"), $q -> h1 ("Error"), $q->p("following err::"), $q->p($q->i($reason)), $q-> end_html; exit; }
<html> <head> <title>Promen</title> </head> <body bgcolor="#E0E0E0" text="#000000" > <h2 align="center"><font face="symap" ><u> <font color="#800000">PromEn</font></a><font color="#800000">: Promote +r prediction using energy differences&nbsp; </font></u></h2> <FORM name="myForm" action = "../cgi-bin/serve.cgi" enctype='multipart +/form-data' method="POST" onsubmit="return validate()"> <p align="right">&nbsp;<p align="left"><b><font color="#800000" face=" +fantasy" size="4" >Upload the sequence file</font><font size="4" > <input type="file" name="fname" size="40">
>EMBOSS_001 gagccggaacaaattgaacaatcctacgccagctgccaaccgtggccggagcaggtggta ttagatggggaacgtgaaacgttaggcctgtacctgaccggacaccctatcaaccagtat ttaaaagagattgagcgttatgtcggaggcgtaaggctgaaagacatgcacccgacagaa cgtggtaaagtcatcacggctgcggggctcgttgttgccgcgcgggttatggtcaccaag cgcggcaatcgtatcggtatctgcacgctggatgaccgttccgggcggctggaagtgatg ttgtttactgacgccctggataaataccagcaattgctggaaaaagaccgcatacttatc gtcagcggacaggtcagctttgatgacttcagcggtgggcttaaaatgaccgctcgcgaa gtgatggatattgacgaagcccgggaaaaatatgctcgcgggcttgctatctcgctgacg gacaggcaaattgatgaccagcttttaaaccgactccgtcagtctctggaaccccaccgc tctgggacaattccagtacatctctactatcagagggcggatgcacgcgcgcggttgcgt tttggcgcgacgtggcgtgtctctccgagcgatcgtttattaaacgatctccgtggcctc attggttcggagcaggtggaactggagtttgactaatacaggaatactatgagtctgaat ttccttgattttgaacagccgattgcagagctggaagcgaaaatcgattctctgactgcg gttagccgtcaggatgagaaactggatattaacatcgatgaagaagtgcatcgtctgcgt gaaaaaagcgtagaactgacacgtaaaatcttcgccgatctcggtgcatggcagattgcg caactggcacgccatccacagcgtccttataccctggattacgttcgcctggcatttgat gaatttgacgaactggctggcgaccgcgcgtatgcagacgataaagctatcgtcggtggt atcgcccgtctcgatggtcgtccggtgatgatcattggtcatcaaaaaggtcgtgaaacc aaagaaaaaattcgccgtaactttggtatgccagcgccagaaggttaccgcaaagcactg cgtctgatgcaaatggctgaacgctttaagatgcctatcatcacctttatcgacaccccg ggggcttatcctggcgtgggcgcagaagagcgtggtcagtctgaagccattgcacgcaac ctgcgtgaaatgtctcgcctcggcgtaccggtagtttgtacggttatcggtgaaggtggt

readmore tags by holli