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

Hello i would like to know how to download a file, like in most web hosting control panels where they have form uploading they also have an option to download the files. (i.e. download a .cgi file out of my user directory).

Any help would be much appreciated.

Replies are listed 'Best First'.
Re: Downloading FIle from local harddrive?
by Zaxo (Archbishop) on Sep 29, 2002 at 07:08 UTC

    That depends on who has access through the server configuration and .htaccess . I usually keep a development tree, and never depend on the production side to provide the source.

    If I need to repair an installation, I require a baseline of all site files with md5sums to immunize me from blame for things I didn't do.

    To provide a source url for a site file, just provide a sub in the admin area to read the file and print it to you with the correct file name and Content-Type.

    (Added) Btw, I don't see what this has to do with local drives.

    After Compline,
    Zaxo

Re: Downloading FIle from local harddrive?
by AcidHawk (Vicar) on Sep 29, 2002 at 08:29 UTC

    I'm not sure what it is you really want to do, but have you thought about setting up an ftp function to get the file when a button on your form is pressed

    Although this is not related to local drives...

    Or could it be that when you press a button and you really want the .cgi script to run, your browser asks you if you would like to download the file..???

    This normally happens, if I remember correctly, when your web server hasn't been setup to execute .cgi files properly


    -----
    Of all the things I've lost in my life, its my mind I miss the most.
Re: Downloading FIle from local harddrive?
by rjimlad (Acolyte) on Sep 29, 2002 at 15:14 UTC

    I presume you mean like a view-source option for the script itself? Well, if it is just for scripts you could do something like:

    use CGI; my $q=new CGI; print $q->header("application/x-perl").`cat $0`;

    Whereas if you're talking about *any* file (eg image files etc) you need to do a bit of juggling with mime types, and of course there's the whole attachment/filename issue also. Something like this would do for that:

    use CGI; my $q=new CGI; my $filename=$q->param("some.param.name"); die "I won't display the file: $filename" unless some_test_or_other($f +ilename); print $q->header(-type=>`file -bi $filename`."; file=$filename", -content_dispostion=>"attachment; filename='$filename'"). `cat $filename`;

    WARNING: Be very very careful if you do something like this. File-download scripts are responsible for a great many web site security holes, so unless you intend to impliment really anal input checking, don't even consider doing a (named) file download script as described above.

    Also please note that using - and trusting - 'cat' and 'file' to do work for you is both inefficient and potentially insecure. They're included in these examples for brevity, in any real environment you should generally use a self-written function or CPAN module as appropriate.

      Heh. Useless use of cat and backticks. Use seek to rewind DATA to position zero.