Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Copying from filehandle:read ?/

by cool (Scribe)
on Aug 24, 2006 at 11:13 UTC ( #569336=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: Copying from filehandle:read ?/
by shmem (Chancellor) on Aug 24, 2006 at 12:25 UTC
    Apart from the standard hint 'check the logs', some code review remarks:

    • First thing, always use -T for CGI scripts. See perlsec.
    • The Content-Type Header is printed twice in case of an error, since you do print "Content-type:text/html\n\n"; at the start of your script and print $q -> header ("text/html"), in sub error
    • There's no submit button in your html file and onsubmit="return validate()" is not really helpful for us to test your script, since there's no JavaScript in it.
    • The variable $fh is undefined. Change $query->upload("$file") to $query->upload('fname'). The upload method expects a CGI param, not it's value.
    • You have a block beginning with if ($file) { - well, fine; but else?
    • In sysopen OUTPUT, UPLOAD_DIR . $file,O_CREAT|O_EXCL you forgot one of the flags O_WRONLY or O_RDWR. So your OUTPUT is only opened for input.

    --shmem

    _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                  /\_/(q    /
    ----------------------------  \__(m.====.(_("always off the crowd"))."
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Copying from filehandle:read ?/
by Anonymous Monk on Aug 24, 2006 at 11:41 UTC

    look into the web server's logs to see what error message you get .

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://569336]
Approved by Sidhekin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2020-02-25 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (113 votes). Check out past polls.

    Notices?