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
267 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now