Solved

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

Posted on 2003-11-26
5
268 Views
Last Modified: 2008-02-01
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!
0
Comment
Question by:dmeyers83
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:petoskey-001
ID: 9841589
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.
0
 

Author Comment

by:dmeyers83
ID: 9875879
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?
0
 
LVL 7

Accepted Solution

by:
petoskey-001 earned 500 total points
ID: 9876096
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.
0
 

Author Comment

by:dmeyers83
ID: 9876321
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.
0
 

Author Comment

by:dmeyers83
ID: 10412653
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 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

16 Experts available now in Live!

Get 1:1 Help Now