Solved

passing file handles.

Posted on 2002-05-12
6
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

724 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