passing file handles.

Dear All,

I need to allow file uploads to a web server, but if a file exists I must give the user the opportunity the cancal, or overwrite the file.

So, at the moment (and this, if possible is the way I'd like to keep it) I have the following form which a user can upload a file with:

<form action="/cgi-bin/upload.cgi" method="post" enctype="multipart/form-data" TARGET=_top>Document to Upload: <input type="file" name="document">
<input type="submit" name="submit" value="Upload Now"
</form>

Here's some of upload.cgi:

$filename = $query->param("document");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("document");

$match=0;

opendir(DIRHANDLE, "/mnt/raid0/supergen/public_html/secure/uploaded_documents") || die "Cannot Open Directory, please email pmitcheson\@iee.org with this error : $!";
foreach $name (sort readdir(DIRHANDLE)) {
        if ($name eq $filename)  {
                $match=1;
        }
}

If the document doesn't already exist (match==0), the following is executed, which works (the file is uploaded):

if ($match == 0){               #if the file name was new do the upload

        open UPLOADFILE, ">$upload_dir/$filename";

        while ( <$upload_filehandle> )
        {
                print UPLOADFILE;
        }

        close UPLOADFILE;

if the document does exist, the user is redirected to a screen with the option of overwriting or canceling their upload.

Here's the cgi printed html for that:

<FORM action="/cgi-bin/noupload.cgi" method=post>
                <INPUT TYPE=submit VALUE="Cancel"></FORM>
<FORM action="/cgi-bin/force_upload.cgi" method=post>
                <INPUT TYPE=hidden name="fileh" VALUE=\"$upload_filehandle\">
                <INPUT TYPE=hidden name="file" VALUE=\"$filename\">
                <INPUT TYPE=submit VALUE="Overwrite"></FORM>

I am trying to put the file name and file handle in the hidden form tags to pass the file name and handle to the force upload script in case the user wants to overwrite their file.  Here's the force_upload:

$query = new CGI;

$filename = $query->param("file");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle =$query->param("fileh");



        open UPLOADFILE, ">$upload_dir/$filename";

        while ( <$upload_filehandle> )
        {
                print UPLOADFILE;
        }

        close UPLOADFILE;

But, this doesn't work.  So, in otherwords, when I pass the file name and handle from one cgi to another with hidden form tags, I can't do a sucessful file upload.

Does anyone have any ideas?

Paul
LVL 1
paulmitchAsked:
Who is Participating?
 
amandeepConnect With a Mentor Commented:
paulmitch,

I am glad that you got it working now :-)

For handling both text and binary files, as I mentioned, use the read function.

REPLACE

while ( <$upload_filehandle> )
       {
               print UPLOADFILE;
       }

WITH

my($bytesread,$buffer);
while($bytesread=read($upload_filehandle,$buffer,1024))
{
   print UPLOADFILE $buffer;
}


Aman.
0
 
amandeepCommented:
paulmitch,

The above scenario of passing file handle from one script to another would not work.

The work around for ur problem is..
1> If the file exists, copy the content to a temporary file..(may be filename.tmp)
2> generate the second form and pass the tmp-filename as hidden parameter.
3> now if the option selected is cancel, then delete the tmp file, else if the option selected is overwrite then overwrite to the existing file by reading the tmp file and delete the tmp file after that.

Note: The above code posted seems to be accepting text file uploads. If binary files are uploaded, it may get corrupt. If u are doubtful about the file-type, use read() function instead.

Hope this helps :-)
Aman
0
 
paulmitchAuthor Commented:
Aman,

Thanks for the advice.  I have got it working now, the way you suggest.

Please could you tell me a bit more (specifically what I need to write) to hyandle binary and text files, and I'll give you the points.

Many thanks for your help.

Paul
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
paulmitchAuthor Commented:
and i have upped the points for your trouble :-)
0
 
paulmitchAuthor Commented:
ah - I can't up it past 500.  sorry.
0
 
paulmitchAuthor Commented:
Great, thanks so much for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.