Transport FTP file from remote server to client PC, HELP!
Moderator: General Moderators
Transport FTP file from remote server to client PC, HELP!
Hi,
Well, the problem is follow:
I have an private ftp server with users files. I'm programming a web interface to view/download these files. First I tried with ftp_get(), but i saw that that funtion only download the files to the folder where the function resides.. I want to download the ftp files to the client machine..
I saw many solutiones over there.. but none of them funtion.. I test a commercial script that make that i want.. but i don´t want to pay only for the right function to download the files..
Can some one help me with this problem??
Txns.
Ezequiel.
Well, the problem is follow:
I have an private ftp server with users files. I'm programming a web interface to view/download these files. First I tried with ftp_get(), but i saw that that funtion only download the files to the folder where the function resides.. I want to download the ftp files to the client machine..
I saw many solutiones over there.. but none of them funtion.. I test a commercial script that make that i want.. but i don´t want to pay only for the right function to download the files..
Can some one help me with this problem??
Txns.
Ezequiel.
- maliskoleather
- Forum Contributor
- Posts: 155
- Joined: Tue May 15, 2007 2:19 am
- Contact:
is your FTP server on the same server as this script? if so, that could make this alot easier.
essentially, what you need to do is use ftp_get to temporarily grab the file and save it to your server (this is what you are already doing). Then you have two directions to go with this. you could try something more complex, and have the script put out the right headers, then use fread on the file and echo that. or your second route would be to just echo a link to the file you just downloaded, which is much simpler, and only adds an extra click to the download process.
an example of the second method:
essentially, what you need to do is use ftp_get to temporarily grab the file and save it to your server (this is what you are already doing). Then you have two directions to go with this. you could try something more complex, and have the script put out the right headers, then use fread on the file and echo that. or your second route would be to just echo a link to the file you just downloaded, which is much simpler, and only adds an extra click to the download process.
an example of the second method:
Code: Select all
// define some variables
$local_file = 'local.zip';
$server_file = 'server.zip';
// 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);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
//we were able to get the file, so lets echo a link to that file - the user will then be able to download it
echo "<a href=\"$local_file\">Download</a>\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);Thanks, but i want to step my server
Thanks for the answer, but i wan't to use my web server as a pipeline... i want to download directly from the ftp server to the client machine.
- maliskoleather
- Forum Contributor
- Posts: 155
- Joined: Tue May 15, 2007 2:19 am
- Contact:
heh. i had to think about that one for a minute... here's what i've got
just be sure to set your headers properly before you echo the data
Code: Select all
function ftp_read($handle, $file) {
ob_end_flush();
ob_start();
$output = fopen('php://output', 'w');
if (!ftp_fget($handle, $output, $file, FTP_ASCII)) die('Unable to read: ' . $remote_file);
fclose($output);
$data = ob_get_clean();
return $data;
}
//example of how to use:
$ftp_handle = ftp_connect('foo.bar.com', 21, 60);
ftp_login($ftp_handle, 'username', 'password');
$data = ftp_read($ftp_handle, 'path/to/remote.file');
echo $data;Thanks for your code but...
Can you explain what the code do?...
This function can store de remote ftp file in the client machine?? or download the file to the web page?
Regards,
Ezequiel.
This function can store de remote ftp file in the client machine?? or download the file to the web page?
Regards,
Ezequiel.
- maliskoleather
- Forum Contributor
- Posts: 155
- Joined: Tue May 15, 2007 2:19 am
- Contact:
- maliskoleather
- Forum Contributor
- Posts: 155
- Joined: Tue May 15, 2007 2:19 am
- Contact:
Code: Select all
function ftp_read($handle, $file) {
ob_end_flush();
ob_start();
$output = fopen('php://output', 'w');
if (!ftp_fget($handle, $output, $file, FTP_ASCII)) die('Unable to read: ' . $remote_file);
fclose($output);
$data = ob_get_clean();
return $data;
}
//example of how to use:
$ftp_handle = ftp_connect('foo.bar.com', 21, 60);
ftp_login($ftp_handle, 'username', 'password');
$data = ftp_read($ftp_handle, 'path/to/remote.file');
header('Content-Type: application/octet-stream');
echo $data;You are a php guru?.. please read this..
Well,
I must to admit that you know php as your hand...
Im a programmer but my speciality are the Data bases...
Can you explain me what do yo mean when you talk about headers?? and make and example code here to view? please dont laught to me
Regards,
Ezequiel
I must to admit that you know php as your hand...
Im a programmer but my speciality are the Data bases...
Can you explain me what do yo mean when you talk about headers?? and make and example code here to view? please dont laught to me
Regards,
Ezequiel
An observation about the code..
As i can see,
the function don't store the entire file extracted from the ftp server in the web server and then, put that in the client machine?...
the function don't store the entire file extracted from the ftp server in the web server and then, put that in the client machine?...
- maliskoleather
- Forum Contributor
- Posts: 155
- Joined: Tue May 15, 2007 2:19 am
- Contact:
Re: You are a php guru?.. please read this..
with web pages, and anything passed over the internet, there are a series of headers generated by the server that say how big the file is, what type of content it is, how its encoded.. things like that. This way, the browser knows how to interperate it. On most servers, the default content-type is text/html, and this is what php echoes data as. When we echo binary data (which in this case our files are), we need to specify what file it is.zvirtual wrote:Can you explain me what do yo mean when you talk about headers??
with php (and a majority of other web languages), you are able to send custom headers, that override the default ones, or specify things not already set. To do this, we use the header() command.
ie:
Code: Select all
header('content-type: image/jpeg');Code: Select all
header('Content-Type: application/octet-stream');im a little confused on what you mean... are you asking how it works, or is there an error?zvirtual wrote:the function don't store the entire file extracted from the ftp server in the web server and then, put that in the client machine?...
I mean if the code is working passing first the info of the ftp file to the web server and then, transmit this to the client machine...im a little confused on what you mean... are you asking how it works, or is there an error?
my MSN is: info@zvirtual.com.ar please if you want contact me to explain well the situation..
But in fee words... the clients store files large than 500mb in their ftp account, and im programming a web interface that permits they download their stored files...
Regards,
Ezequiel[/quote]