Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
278 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
[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
  • 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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

730 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