Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

CGI-Upload / Bad File Number

by frnk (Novice)
on Jul 16, 2016 at 12:21 UTC ( [id://1167877]=perlquestion: print w/replies, xml ) Need Help??

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

Hi there,

i have a problem with a quiet simple file-upload script.

Background: I've two websites both on the same server in different folders. I took the (working) script from one site with some small changes to use it also in the other one, but i cant get it to work.

Here ist the code:
sub Upload { my $dst = shift; my $src = shift; my $dat +a; my $ext = $src; $ext =~ s/^.*\.//; $dst .= ".$ext"; open (DATEI,'>',$dst) or die 'Error processing file: ',$!; binmode $src; binmode DATEI; # --- Datei hochladen while(read $src,$data,640000) { print DATEI $data; } close DATEI; }

The result is a zero-byte file. The problem appears at binmode $src;. After this line $! contains 'Bad file number'.

I tried to comment out this line but then the error appears after calling read().

Any ideas???

Regards, Frank

Replies are listed 'Best First'.
Re: CGI-Upload / Bad File Number
by poj (Abbot) on Jul 16, 2016 at 13:02 UTC

    Where and how are you assigning the $src parameter ? It looks like you may be confusing a filehandle with a filename.

    poj
Re: CGI-Upload / Bad File Number
by Laurent_R (Canon) on Jul 16, 2016 at 13:49 UTC
    You don't appear to open the input file anywhere, and, as mentioned by poj, you're using $src as a file name in the first part of your program, and as a file handle in the second part.
      I'm new here and i did not saw the reply-button. So i postetd my reply as answer on my own post - sorry... You'll find it below
Re: CGI-Upload / Bad File Number
by frnk (Novice) on Jul 16, 2016 at 15:05 UTC

    Hi,

    thanks for your suggestions.

    '$src' contains the name of the file to be uploaded (i.e. '123.jpg'). The value comes from a html-formular.

    I want to upload a file, so - as far as i know - i don't have to open it and there is no handle. But i have to admit, that i don't really aunderstand how uploading-process works.

    As alredy mentioned, i use a simelar code in another script, where it works fine. Below is the working function (comments are translated) for comparation...

    if($file=~/[\w _\.\-\(\)\+]+\.jpg/i) { # --- Zieldateiennamen erstelle und Datei öffnen => engl.: c +reate destination-file-name and open $sname = time.'.'.$ENV{'REMOTE_ADDR'}.' - '.$ueberschrift.' - '. +$unterschrift.' - '.$file; $sname =~ s/ä/ae/g; $sname =~ s/Ä/Ae/g; $sname =~ s/ö/oe/g; $sname =~ s/Ö/Oe/g; $sname =~ s/ü/ue/g; $sname =~ s/Ü/Ue/g; $sname =~ s/ß/ss/g; $fname = '../Content/Gaestebuch/'.$sname; open DAT,'>'.$fname or die 'Error processing file: ',$!; # --- Dateien in den Binaer-Modus schalten => engl.: switc +h to bin-mode binmode $file; binmode DAT; # --- Datei hochladen => engl.: Upload my $data; while(read $file,$data,640000) { print DAT $data; } close DAT; }

    As you see, also here '$file' (= '$src' in the other function) is not a handle.

      i don't really understand how uploading-process works.

      If you using CGI, see CGI, in particular the section on Older ways to process uploads

      The original way to process file uploads with CGI.pm was to use param(). The value it returns has a dual nature as both a file name and a lightweight filehandle.
      

      You should find your working script will fail if you force $file into scalar by adding the line

      if($file=~/[\w _\.\-\(\)\+]+\.jpg/i) { $file = ''.$file;

      Does $src have some added processing that $file doesn't. ?

      poj

        This is how i read the CGI-data:

        $query = new CGI; @names = $query->param; foreach (@names) { $val = $query->param($_); eval("\$$_ += '$val';"); }

        There is one value called '$file1' containing the source-file.

        The upload-function is called this way:

        if ($FNC eq "Hochladen") { if ($file1 ne "") { Upload("$DIR$FIL +E/_file1", $file1); } }
        ...where '$file1' is named '$src' in the function and "$DIR$FILE/_file1" is the destination filename without extension, which is taken from the source filename.

        There is no other apperence of '$file1' or '$src' in my code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found