Solved

passing file handles.

Posted on 2002-05-12
6
246 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now