-1

I have try some code to download file using FTP, But File not send in computer client. this code is just copy file from another folder in server computer. Could you show me the solution. Here is my code:

<?php
//FTP File Download
$file_name      = 'myfile.xml';
$destination    = '/download/'.$file_name;
$ftp_user_name  = 'chelsea';
$ftp_user_pass  = 'drogba';
$ftp_server = '192.168.1.1';
$conn_id    = ftp_connect($ftp_server);
$login_result   = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

ftp_nb_get($conn_id, $destination, $file_name, FTP_BINARY);
ftp_close($conn_id);
?>

3 Answers3

0

be sure The Ftp server Allow to Get file Server-to-server if not allow you cant use this function.

and test This code

<?php

// define some variables
$local_file = '<save-file-as>';
$server_file = '<server-file-name>';
$server_directory='/';
$ftp_user_name='<ftp-user>';
$ftp_user_pass='<ftp-pass>';
$ftp_server='<ftp-host-name>';
// 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);

/* uncomment if you need to change directories*/
if (ftp_chdir($conn_id, $server_directory)) {
    echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else { 
    echo "Couldn't change directory\n";
}


// 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 "There was a problem\n";
}

// close the connection
ftp_close($conn_id);

?>
mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22
  • the code is running well, no error. but just copy file from one folder to another folder in server computer. not send in client computer... if the FTP server must set to get File server-to-server how to set that function. – Niken Swandarini Jan 23 '13 at 08:23
  • in FTP Server Config you Can find setting for This. also you can Test this class [Ftp](http://www.phpclasses.org/package/1743-PHP-FTP-client-in-pure-PHP.html) It Must Work. – mohammad mohsenipur Jan 23 '13 at 08:49
0

Try using curl to download a file from ftp

$curl = curl_init();
$file = fopen("myfile.xml", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.yourdomain.com/myfile.xml"); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
curl_exec($curl);
curl_close($curl);
fclose($file);

PHP: download any file from ftp-server to harddrive?

Community
  • 1
  • 1
Manigandan Arjunan
  • 2,260
  • 1
  • 25
  • 42
0

After the script downloaded the file from the second server to the one running the script, you can send it to the client with readfile and the appropiate headers (see here: Headers used to download file php )

Of course it will get sent via HTTP because the request to the PHP script was a HTTP request and you cannot change the protocol on the fly.

Community
  • 1
  • 1
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111