By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,656 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Download to local after ftp_get()

100+
P: 162
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. // define some variables
  3. $ftp_server = "123.45.67.890";
  4. $local_file = '/scratch1/test/'.$name;
  5. $server_file = $name;
  6.  
  7. echo "server file: $server_file <br>";
  8.  
  9. $ftp_user_name = "5a515";
  10. $ftp_user_pass = "m5n5o5";
  11.  
  12. // set up basic connection
  13. $conn_id = ftp_connect($ftp_server);
  14.  
  15. // login with username and password
  16. $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
  17.  
  18. if ($login_result) echo "Successfully connected<br>";
  19.  
  20. //change directory
  21. ftp_chdir($conn_id, "tts");
  22. ftp_chdir($conn_id, "incoming");
  23.  
  24. // try to download $server_file and save to $local_file
  25. if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)){
  26.  
  27.     echo "Successfully written to $local_file\n";
  28.  
  29. }else{
  30.  
  31.     echo "<br>There was a problem\n";
  32. }
  33.  
  34. // close the connection
  35. ftp_close($conn_id);
  36.  
  37.  
Mar 2 '08 #1
Share this Question
Share on Google+
2 Replies


dlite922
Expert 100+
P: 1,584
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. // define some variables
  3. $ftp_server = "123.45.67.890";
  4. $local_file = '/scratch1/test/'.$name;
  5. $server_file = $name;
  6.  
  7. echo "server file: $server_file <br>";
  8.  
  9. $ftp_user_name = "5a515";
  10. $ftp_user_pass = "m5n5o5";
  11.  
  12. // set up basic connection
  13. $conn_id = ftp_connect($ftp_server);
  14.  
  15. // login with username and password
  16. $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
  17.  
  18. if ($login_result) echo "Successfully connected<br>";
  19.  
  20. //change directory
  21. ftp_chdir($conn_id, "tts");
  22. ftp_chdir($conn_id, "incoming");
  23.  
  24. // try to download $server_file and save to $local_file
  25. if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)){
  26.  
  27.     echo "Successfully written to $local_file\n";
  28.  
  29. }else{
  30.  
  31.     echo "<br>There was a problem\n";
  32. }
  33.  
  34. // close the connection
  35. ftp_close($conn_id);
  36.  
  37.  
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
Mar 2 '08 #2

100+
P: 162
Thanks dlite.

This is the solutions. Thanks to NogDog.
Expand|Select|Wrap|Line Numbers
  1. // try to download $server_file and save to $local_file
  2. if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)){
  3.     header('Content-Length: '. filesize($local_file));
  4.     header('Content-Type: application/octet-stream');
  5.     header('Content-Disposition: attachment; filename="'.basename($local_file).'"');
  6.     header('Content-Transfer-Encoding: binary');
  7.     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  8.     readfile($local_file); // send the file
  9.     exit;  // make sure no extraneous characters get appended
  10. }
  11.  
Mar 2 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.