Download to local after ftp_get()

Discussion in 'PHP' started by cesarcesar, Mar 1, 2008.

  1. #1
    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.

     
    // 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($ftp_server);
     
    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
     
    if ($login_result) echo "Successfully 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 "Successfully written to $local_file\n";
     
    }else{
     
        echo "<br>There was a problem\n";
    }
     
    // close the connection
    ftp_close($conn_id);
     
     
    Code (text):

     
    cesarcesar, Mar 1, 2008 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    So you have a php file on a webhost. It downloads from a secure FTP server. Once it downloads, you want it to send the file to another location? Ok, if that's right and making the assumption that the php file is called by the users browser. You would have to finish the download and resend the file to them probably as a redirect since you would have to show something while the download finished. Here's a page with how to force the File Download display. If you are wanting it to download without prompting, that isn't feasible.

    http://elouai.com/force-download.php
     
    shallowink, Mar 1, 2008 IP
  3. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i want it to promt of course. the user clicked on a link to down load the video. i just need to move the file, then download to user after approval.

    Checking the link out now. Thanks.
     
    cesarcesar, Mar 1, 2008 IP
  4. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is the solutions. Thanks to NogDog.
    
    // 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
    }
    
    Code (markup):
     
    cesarcesar, Mar 2, 2008 IP