473,659 Members | 3,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Download to local after ftp_get()

162 New Member
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
2 4980
dlite922
1,584 Recognized Expert Top Contributor
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
empiresolutions
162 New Member
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

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

Similar topics

1
1604
by: toedipper | last post by:
Hello, php and mysql I have the code below. It's a mixture of my hand coding and dreamweaver created code. It's meant to log a download and then start the download. I can do the first part ok as I can check the db and see that there is an entry but the download won't start, it just sits there and nothing downloads.
11
1665
by: Jim Hubbard | last post by:
Assuming Microsoft want .Net to be the next big thing in Microsoft programming, why is the .Net Framework 1.1 an optional download on the Windows Update site? I'd think that it should be required. Jim Hubbard
5
6049
by: Ian Davies | last post by:
Hello I am trying to use php to automate the copying of a file from my remotely hosted directory to my computer. I have put together the following but it will only do the copying from one directory to another on the remote server. Reading the postings on these functions they suggest that it can be done but it appears to be the case if your are hosting your site on the same machine you want to copy the files to. Does anyone know if there...
16
7362
by: Bob Moore | last post by:
We have a requirement to put a client onto machines intended for some telephony work. The database access will be via ASP.Net pages, and that's fine, but the telephony stuff requires access to some legacy servers. I've prototyped the client itself as Winforms/C# (which was NOT fun for a C++ curmudgeon like me, but I'll live). The problem is, I'd like to be able to ensure that the user is always running the latest client (our current fat...
5
5521
by: Alex | last post by:
I'm trying to make simple ftp client, used the following article for base: http://www.devarticles.com/c/a/PHP/Building-An-FTP-Client-With-PHP/3/ All works, I can login, see list of files, but except ftp_put and ftp_get functions. They return always false. I tried to set passive mode, it doesn't help. Any advice will be appreciated.
7
2062
by: clintonG | last post by:
To all Microsoft partners and customers who have been unable to download recently or access ASP.NET documentation from the msdn2 website and for all of those customers who have been lied to and misled by some of the sleazy MVPs and the lying cockroaches that Microsoft has working for the company... Microsoft has serious problems with their servers and websites. The entire MSDN server farm and all download resources MSDN manages has been...
1
1771
by: irfan.mohammed | last post by:
Dear All, I need your help,i am using ftp_get for getting the file from remote machine.now i am able to save only at one location.i want the user to select the directory any where in the computer and able to save it. Thanks in advance Irfan
13
4405
unicorn
by: unicorn | last post by:
Hi guys, I have moved my site from one domain to another. it worked perfectly on old domain, ftp_put(), ftp_delete() - i.e. FTP upload and remove scripts work fine, but... i have problem using ftp_get() wicth shows me error, and says : Warning: ftp_get() : Error opening 4.gif in /home/mydomain/public_html/down_att.php on line 43 Couldn't download file '4.gif' . i have checked connection, path. well if i change path (ftp...
5
3120
by: empiresolutions | last post by:
I have some scripts that when initiated by a link click will FTP a 1G+ file from a secure server to another server using ftp_get(). Once ftp_get() is complete i then user Content Headers to open a "save as" dialog box. (working good to this point). Since the files are so large there will be times when a user loses connection or something else that interrupts the download happens. Now what i need is to be able to "finish" the download from the...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.