Download to local after ftp_get()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • empiresolutions
    New Member
    • Apr 2006
    • 162

    Download to local after ftp_get()

    I am using ftp_get to transfer a file from a secure location, to a temp location. (working)

    Once at the temp location i need to have the file automatically download to the clients local machine. Something like a right click "save as" action. How do i do this? I'm thinking its done with headers but not sure.

    My Current Code is below. Thanks for all responses.

    [code=text]
    // define some variables
    $ftp_server = "123.45.67.890" ;
    $local_file = '/scratch1/test/'.$name;
    $server_file = $name;

    echo "server file: $server_file <br>";

    $ftp_user_name = "5a515";
    $ftp_user_pass = "m5n5o5";

    // set up basic connection
    $conn_id = ftp_connect($ft p_server);

    // login with username and password
    $login_result = ftp_login($conn _id, $ftp_user_name, $ftp_user_pass) ;

    if ($login_result) echo "Successful ly connected<br>";

    //change directory
    ftp_chdir($conn _id, "tts");
    ftp_chdir($conn _id, "incoming") ;

    // try to download $server_file and save to $local_file
    if (ftp_get($conn_ id, $local_file, $server_file, FTP_BINARY)){

    echo "Successful ly written to $local_file\n";

    }else{

    echo "<br>There was a problem\n";
    }

    // close the connection
    ftp_close($conn _id);

    [/code]
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Originally posted by empiresolutions
    I am using ftp_get to transfer a file from a secure location, to a temp location. (working)

    Once at the temp location i need to have the file automatically download to the clients local machine. Something like a right click "save as" action. How do i do this? I'm thinking its done with headers but not sure.

    My Current Code is below. Thanks for all responses.

    [code=text]
    // define some variables
    $ftp_server = "123.45.67.890" ;
    $local_file = '/scratch1/test/'.$name;
    $server_file = $name;

    echo "server file: $server_file <br>";

    $ftp_user_name = "5a515";
    $ftp_user_pass = "m5n5o5";

    // set up basic connection
    $conn_id = ftp_connect($ft p_server);

    // login with username and password
    $login_result = ftp_login($conn _id, $ftp_user_name, $ftp_user_pass) ;

    if ($login_result) echo "Successful ly connected<br>";

    //change directory
    ftp_chdir($conn _id, "tts");
    ftp_chdir($conn _id, "incoming") ;

    // try to download $server_file and save to $local_file
    if (ftp_get($conn_ id, $local_file, $server_file, FTP_BINARY)){

    echo "Successful ly written to $local_file\n";

    }else{

    echo "<br>There was a problem\n";
    }

    // close the connection
    ftp_close($conn _id);

    [/code]
    You're on the right track:

    FIRST OF ALL. I really hope that's not the username and password but a fake one, if not you're secure location is not so secure now. Besides FTP is not encrypted anyway. But I digres.

    manipulating the headers to the type of file is exactly what you need to do.

    If that file is public, just copy it to a directory in your websiter and give the public the link to the file.

    if it's private (based on user) then you need to change the header to whatever type of file it is, use php fread() to read the file, print it, and voila!

    ...or so I think in theory. I'm no PHP master but that's how I'd tackle it.

    Good Luck

    Comment

    • empiresolutions
      New Member
      • Apr 2006
      • 162

      #3
      Thanks dlite.

      This is the solutions. Thanks to NogDog.
      Code:
      // try to download $server_file and save to $local_file
      if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)){
          header('Content-Length: '. filesize($local_file));
          header('Content-Type: application/octet-stream');
          header('Content-Disposition: attachment; filename="'.basename($local_file).'"');
          header('Content-Transfer-Encoding: binary');
          header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
          readfile($local_file); // send the file
          exit;  // make sure no extraneous characters get appended
      }

      Comment

      Working...