Solved

passing file handles.

Posted on 2002-05-12
6
249 Views
Last Modified: 2008-03-04
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
0
Comment
Question by:paulmitch
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:amandeep
ID: 7004767
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
 
LVL 1

Author Comment

by:paulmitch
ID: 7004936
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
 
LVL 1

Author Comment

by:paulmitch
ID: 7004937
and i have upped the points for your trouble :-)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 1

Author Comment

by:paulmitch
ID: 7004938
ah - I can't up it past 500.  sorry.
0
 
LVL 5

Accepted Solution

by:
amandeep earned 500 total points
ID: 7007036
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
 
LVL 1

Author Comment

by:paulmitch
ID: 7007042
Great, thanks so much for your help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question