Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Uploading a file

by wonko (Pilgrim)
on Jun 08, 2000 at 15:37 UTC ( #17055=perlquestion: print w/replies, xml ) Need Help??

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

I give up. I've tried to get this to work, but i can't. What I want to do is to upload a picture (jpg, gif, png) and put it in a directory on a web page. The goal is to build a administrating tool to a web page, so HTML illitterals can maintain it. The script woud probably run on LINUX and the picture _can_ be placed there, but even better woud be if it coud be placed on the WINNT server the main page is placed on. Another way to do it is to run the scripts on a local machine and then FTP it to the WINNT server. Can you help me on this?


Replies are listed 'Best First'.
(jcwren) Re: Uploading a file
by jcwren (Prior) on Jun 08, 2000 at 16:45 UTC
    What, exactly, is the question? Are you looking for code to do this, an explanation of the framework for how to do this, or general ideas for the user interface?

    I recently had to do something similiar. I manage (such as one does) a large database of batteries. Users need to be able to upload pictures of batteries in to the database. There is a page with a couple of fields on, where the user enters the serial number, describes the picture (intact, exploded, top view, whatever), and, most importantly, the path to the file on their local machine. This is done using a INPUT field, with a type of FILE.

    When the user clicks 'Go', the file will be submitted as part of the POST data. Note that the ENCTYPE on the FORM with the INPUT button must be set to "multipart/form-data".

    Let's say that your input field is named 'photofile'. The following code
    my $fn = $html->param ('photofile'); my $fh = $html->upload ('photofile');
    gets the filename the user wants to upload from his local drive, and a file handle to said file. You then proceed to use a loop
    # # This could get a little hairy. We don't know how big a picture +the user i # give us. We're going to limit him to 512K. That should cover a +nything we # of. # while ((length ($photo) <= (512 * 1024)) && read ($fh, $buffer, 102 +4)) { $photo .= $buffer; } if (length ($photo) == (512 * 1024)) { print_file ("File too big!"); return; }
    to read the data into a string. Now I perform some other processing on the file data before it's loaded into the database, but you could copy it straight to a file (I pump the image through ImageMagick to create a thumbnail, do some normalization, and limit the dimensions of the picture). You *absolutely* need to set a limit on how much data you'll accept or some weenie may try to upload a 1,000,000 x 1,000,000 x 24 bit image of Britney Spears to see how you'll handle it.

    This is the basic form of what you're trying to do. If you can clarify the question some, a little more specific help can probably be provided.

      Hmmm ... 1,000,000 x 1,000,000 x 24 bit image of Britney Spears!!!
Re: Uploading a file
by Russ (Deacon) on Jun 08, 2000 at 16:28 UTC
    So, are you having trouble with the upload (HTML tags, perl script to receive it), the server config (permissions for nobody to write to the directory) or the NT part (getting the file from here to there)?

    If the problem is the perl to receive the upload, try this:

    sub doUpload{ my $FileHandle = CGI::param('UploadName'); open OUTFILE, '>TheFileName' or die "Couldn't open output file: $!\n +"; while ($Bytes = read($FileHandle,$Buffer,1024)) { $BytesRead += $Bytes; print OUTFILE $Buffer; } warn "Received $BytesRead bytes"; close OUTFILE; }
    I didn't pre-declare my variables, and I used $Bytes just to demonstrate something to do with read()'s return value. You get the idea from this, I hope.

    If this doesn't answer your specific question, post an update and I'll see if I can help more...


RE: Uploading a file
by PipTigger (Hermit) on Jun 08, 2000 at 16:47 UTC
    I just encountered this issue yesterday. The html which contains the form and the upload option must be defined like: <form enctype="multipart/form-data" method="post">You can include an input field within the form that has a type="file" which will allow the file to be uploaded to the CGI script. I struggled with the handling of it but the link above details how I finally werked it out. As chromatic noted, I don't use the object notation. I use :standard which causes my namespace to inherit everything from ... I like it that way but it's probably because I'm a rookie with objects. If you need any further direction, I'll be glad to post more of my code if you'd like. TTFN & Shalom.


    p.s. Byslexia is a Ditch!
Re: Uploading a file
by wonko (Pilgrim) on Jun 11, 2000 at 18:33 UTC
    Okay, now I can upload the file, I can wiew it in the browser (if i just print $buffer)
    my $FileHandle = $query->param('bild'); print "$filehandle"; open OUTFILE, '>huvudnyhet.jpg' or die "Couldn't open output file: $ +!\n"; while ($Bytes = read($FileHandle,$Buffer,1024)) { $BytesRead += $Bytes; print OUTFILE $Buffer; } warn "Received $BytesRead bytes"; close OUTFILE;
    but the file i save is somehow corrupt, i can not open it either in the browswer or in Photoshop. ('Coud not open the document because of a problem parsing the JPEG data'). Suggestions? :) /wonko
      I think you need  binmode OUTFILE;after your open statement.


        Thanks! now it works!


        I'm having the same problme but what is binmode OUTFILE;? Robert
Re: Uploading a file
by Ovid (Cardinal) on Jun 09, 2000 at 00:35 UTC
    I don't know if you're using Apache, but I figured I'd throw this on the ground for people to gawk at. What Web server are you using? I had a similar problem trying to upload binary data using a local installation of Apache on a Windows box (forgive me) and Active State Perl. Every time I tried to upload a file, it was corrupted. When I ported the same script over to Linux, my only change was the shebang line and the script worked perfectly. From what I read at, Apache for Windows is a beta product and I stepped on a bug. Hope this helps.
Re: Uploading a file
by wonko (Pilgrim) on Jun 09, 2000 at 12:22 UTC
    Thanks for all your help, I will try your suggestions. I am using Xitami as my local web server. It is running on WINNT. The solution i finally decided to use is to run the scripts local and then FTP the HTML code and pictures to the production server, which is IIS 4.0 on winnt. This way i can have the administrator local, and don't have to think too much of security (I can eiter make the server acessable only to our IP adresses or give it a private IP number) Making the perl scripts FTP the stuff will make the process transparent for the users also.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2020-10-19 16:35 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (205 votes). Check out past polls.