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

Hi! I'm trying to import Netscape type bookmark files into a mysql database table using a cgi script, but having a bit of trouble.

I use start_multipart_form to get a file from main program interface, then parse it in a sub called start_import, using HTML::TokeParser to make sure its really a bookmark file, find out how many links and categories it has, and set a few datafields like import date etc. If all is ok, i call another sub called finish_import where I do the inserts.

The script works fine on my home computer, but when I put it up on the server, i get an error from TokeParser at the beginning of finish_import telling me "no such file or directory." In other words, I pass file from main to start_import parse file successfully, then lose file going from start_import to finish_import. Yet I try to do the same thing from main to start and from start to finish.

Main to start I use start_multipart_form and filefield; in start I then get $filename from param(uploaded_file) and feed it to TokeParser; if all is well, I put $filename in a hidden field called uploaded_file and call finish_import; finish import feeds uploaded_file to TokeParser, which then complains as above. I've been working on this for a couple of days now and no dice. (I'm not the fastest worker in the world)

Any ideas?

Replies are listed 'Best First'.
Re: passing fileuploads to more than 1 sub
by PodMaster (Abbot) on Jul 18, 2003 at 16:26 UTC
    Yeah. You're obviously getting some kind of error message, so just track it down further. Besides that (and without any code), follow : Its really unclear as to what you're doing. You say $filename in a hidden field called uploaded_file so is this a second request? Browsers generally don't let you specify a filename, cause /usr/etc/passwd;

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: passing fileuploads to more than 1 sub
by cfreak (Chaplain) on Jul 18, 2003 at 18:05 UTC

    If I understand you correctly you are trying to put the name of the file in a hidden input on an HTML form and expecting to get that file back. It doesn't work that way, file upload fields in the browser are special, they actually send the data and the user is forced to pick the file themselves (you can't even put a default value in.) This is to ensure someone doesn't force a user to upload sensitive files to the net.

    Really I don't understand why you are trying to do it in two parts at all. Why can't you just check the bookmark file for validity, save it in a temporary place on your server, get HTML::TokeParser to immediatly open it and parse it how you want and then return a success or failure message to the browser. You could then unlink the uploaded file so it isn't sitting around on your server.

    Lobster Aliens Are attacking the world!

      Yes, I see. You can not pass a file by putting a filename in a hidden input and then telling the browser to grab the file; that would mean people could grab stuff off your computer at will. A bad thing.

      Why break it down into start_import and finish_import? To present the user with information about the file he selected and allow for further decisions/input based on that before inserting the whole mess into a table.

      Saving to a temp file plus some restructuring of my script's logic sounds like the solution. The initiate thanks his fellow monks and proceeds on the long path of enlightenment for perl-impaired bozos.

Re: passing fileuploads to more than 1 sub
by sgifford (Prior) on Jul 18, 2003 at 16:34 UTC
    It's also worth keeping in mind that the variable that (IIRC, at least)CGI returns for a file upload is a magical variable that is both a filename and a filehandle. If you are doing something strange with it, it may lose these magical properties.