Having the user define the folder to which they want to download a selected file. Using FTP functions

Hello Experts!  I'm a Intern for a IT company and I'm creating a software upgrade download panel for them and I everything was going great till I hit this one glitch.  I couldn't have a user a define where they want to download.  At first I tried to look for a solution that will let the user browse for the file within their page such as <input type="file" name="test"> but edited some how with javascript, active x, but there really was no good solution except use asp which I'm trying to stay away from.   So I was wondering if anybody knows a way to have the user define the folder they want to download to by invoking some sort of SaveAs prompt or any way that is clear and easy that will give the string location such as "C:/windows/temp".

I'm using this pretty cleancut code for the get.

if (ftp_get($conn_id, "$LocalHarddrive", $filename, FTP_BINARY)) {
    echo "Successfully downloaded CLICK HERE TO CLOSE";
} else {
    echo "There was a problem please contact the server administrator\n";
}

I'm new to this site, and I'm hoping to become a active member.  Thank you everybody for your time!
dmeyers83Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petoskey-001Commented:
Unfortunately You can't suggest a location for the user to save the file to on their own hard drive, the best you can do is suggest a filename.

Also, when using the ftp_get command, the $LocalHarddrive is the location of the file on THE SERVER.  The client browser still has not downloaded any files.  Once you download the file from the FTP server to the Web server running PHP, you can have the user download it to their hard drive - but as stated you can't tell them where to put it.

<?php
function DownloadFile($filename)
{
    // Check filename
    if (empty($filename) || !file_exists($filename))
    {
        return FALSE;
    }
    // Create download file name to be displayed to user
    $saveasname = basename($filename);
    // Send binary filetype HTTP header
    header('Content-Type: application/octet-stream');
    // Send content-length HTTP header
    header('Content-Length: '.filesize($filename));
    // Send content-disposition with save file name HTTP header
    header('Content-Disposition: attachment; filename="'.$saveasname.'"');
    // Output file
    readfile($filename);
    // Done
    return TRUE;
}

if (ftp_get($conn_id, "$LocalHarddrive", $filename, FTP_BINARY)) {
    if (DownloadFile($LocalHarddrive)) {
        echo "Successfully downloaded CLICK HERE TO CLOSE";
    } else {
        echo "An error occurred downloading the file!";
    }
} else {
    echo "There was a problem please contact the server administrator\n";
}

?>

Is this right, or did I misunderstand something.
dmeyers83Author Commented:
Wow thanks!  Thats what I wanted but in my code it downloads to from the ftp to the users local machine.

Yours downloads to the webserver from the ftp, then to the local machine.  Any way to skip the middle man?
petoskey-001Commented:
The simplest method would just be to give the user a URL to the FTP site.  This would send them directly there and allow them to download the file.

The format for an FTP url is  ftp://user@password:the.doman.name/path/to/the/file.txt

You don't need the user / password for anonymous FTP.  You will want to make sure that whatever user/password you use has READ ONLY properties to the FTP site.

By using a direct FTP link, you miss out on any HTTP authentication.  You only have the FTP sites name and password.  Also using this method, you still won't be able to suggest a file name, or location on the users machine.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dmeyers83Author Commented:
I thought of that but the problem was that I'm storing expsenive software, depending what the user purchased it checks the database and it displays a list of choices they have to download.  Depending what the user downloads it gives enough information for the download php script to locate the file without the person ever knowing where its from or the user name and password.  

It works perfectly the way I did it but I just really need to have the save as prompt come up and have the user choose where they want to download the file, and you provided me with some interesting code:

    // Create download file name to be displayed to user
    $saveasname = basename($filename);
    // Send binary filetype HTTP header
    header('Content-Type: application/octet-stream');
    // Send content-length HTTP header
    header('Content-Length: '.filesize($filename));
    // Send content-disposition with save file name HTTP header
    header('Content-Disposition: attachment; filename="'.$saveasname.'"');
    // Output file

it appears that it should provide the "SaveAs" prompt then assign the local harddrive information in the filename variable.  Then I could just use that in my "ftp get function".  I'm trying to fool around with your code, but I'm terrible with headers!  I get all these P string and Tstring evers in them.
dmeyers83Author Commented:
After working trying to figure out the maze of different sorts of headers, the browser does automate them with a ftp wrapper and keep the password and username secure.  Also if the link of the file is within the php the user does not know where its downloading from.  Although it is not nesscarly what I was looking for it worked out fine.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.