Transport FTP file from remote server to client PC, HELP!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zvirtual
Forum Newbie
Posts: 6
Joined: Mon May 21, 2007 12:01 pm

Transport FTP file from remote server to client PC, HELP!

Post by zvirtual »

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.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

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:

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);
zvirtual
Forum Newbie
Posts: 6
Joined: Mon May 21, 2007 12:01 pm

Thanks, but i want to step my server

Post by zvirtual »

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.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

heh. i had to think about that one for a minute... here's what i've got

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;
just be sure to set your headers properly before you echo the data
zvirtual
Forum Newbie
Posts: 6
Joined: Mon May 21, 2007 12:01 pm

Thanks for your code but...

Post by zvirtual »

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.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

it reads the file from the ftp, then echos the data it reads. assuming you send the proper headers (executable, pdf, etc), the client should be able to save the file right to their machine.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

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;
use this instead. no dealing with headers and stuff... it should force the browser to display a saveas box
zvirtual
Forum Newbie
Posts: 6
Joined: Mon May 21, 2007 12:01 pm

You are a php guru?.. please read this..

Post by zvirtual »

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 :oops:

Regards,

Ezequiel
zvirtual
Forum Newbie
Posts: 6
Joined: Mon May 21, 2007 12:01 pm

An observation about the code..

Post by zvirtual »

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?...
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Re: You are a php guru?.. please read this..

Post by maliskoleather »

zvirtual wrote:Can you explain me what do yo mean when you talk about headers??
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.

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');
our other trick is to use the "application/octet-stream" content-type to force a save as box. This tells the browser that its just binary data being served, and we should just save it. hence why i added the following line to the code

Code: Select all

header('Content-Type: application/octet-stream');
as for
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?...
im a little confused on what you mean... are you asking how it works, or is there an error?
zvirtual
Forum Newbie
Posts: 6
Joined: Mon May 21, 2007 12:01 pm

Post by zvirtual »

im a little confused on what you mean... are you asking how it works, or is there an error?
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...
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]
Post Reply