how to get file from ftp server and copy on own server - php

I want to get file from client server and copy them on my server , I have successfully connected to client server, my code is below.
// connect and login to FTP server
$ftp_server = "xx.xxx.xxx.xxx";
$ftp_username = 'xxxxxxxxxxxxxxx';
$ftp_userpass = 'xxxxxxxxxxxxxxxx';
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "<pre>";
print_r($login);
echo "</pre>";
// get the file list for /
$filelist = ftp_rawlist($ftp_conn, "/");
// close connection
ftp_close($ftp_conn);
echo "<pre>";
print_r($filelist);
echo "</pre>";
// output $filelist
var_dump($filelist);
May anyone please advise how can I achieve this?

You can use the ftp_fget function specified here: http://php.net/manual/en/function.ftp-fget.php
(ftp_fget() retrieves remote_file from the FTP server, and writes it to the given file pointer.)
Here an example provided by the documentation:
<?php
// path to remote file
$remote_file = 'somefile.txt';
$local_file = 'localfile.txt';
// open some file to write to
$handle = fopen($local_file, 'w');
// 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 $remote_file and save it to $handle
if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0)) {
echo "successfully written to $local_file\n";
} else {
echo "There was a problem while downloading $remote_file to $local_file\n";
}
// close the connection and the file handler
ftp_close($conn_id);
fclose($handle);
?>

This is how I resolved this now all files will copy on your server. use ftp_ssl_connect if its secure
$ftp_server = "xx.xxx.xxx.xxx";
$ftp_username = 'xxxxxxxxxxxxxx';
$ftp_userpass = 'xxxxxxxxxxxxxxxxxxx';
$ftp_conn = ftp_ssl_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn,pasv);
$output_directory="files1/ftpgetfiles/137/";
// get the file list for /
$filelist = ftp_nlist($ftp_conn, "/");
foreach ($filelist as $key => $value) {
$fp = fopen($output_directory.$value,"w");
if(ftp_fget($ftp_conn, $fp, $value, FTP_BINARY))
{
fclose($fp);
}
}
ftp_close($ftp_conn);

Related

ftp_get() working on localhost but not on live server

<?php
error_reporting(E_ALL);
set_time_limit(300);//for setting
$path='/PickUpOld';
$ftp_server='xxxxxx.com';
$ftp_server_port="xx";
$ftp_user_name='xxxxxxx';
$ftp_user_pass="xxxxxxxx";
// set up a connection to ftp server
$conn_id = ftp_connect($ftp_server, $ftp_server_port);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$ps = ftp_pasv($conn_id, true);
// check connection and login result
if ((!$conn_id) || (!$ps)) {
echo "Fail</br>";
} else {
echo "Success</br>";
// enabling passive mode
ftp_pasv( $conn_id, true );
// get contents of the current directory
$contents = ftp_nlist($conn_id, $path);
// output $contents
$local_file = 'rana.php';
$server_file = 'PickUpOld/test.php';
if ( ftp_get( $conn_id, $local_file, $server_file, FTP_BINARY ) ) {
echo "WOOT! Successfully written to $local_file\n";
}
else {
echo "Doh! There was a problem\n";
}
}
// close the FTP connection
ftp_close($conn_id);
?>
I am trying to copy files one server to another. This Script working fine on localhost, but not working on Live server.
Please share your thoughts where i am doing wrong.
Thanks,

ftp_connect error (download file from FTP)

I'm trying to download file via php script, but unfortunately with this script i get error:
PHP Warning: ftp_connect(): php_network_getaddresses: getaddrinfo
failed: nodename nor servname provided, or not known in
/Users/apple/projects/asystem/download_dump.php on line 15
i try to put different FTP address test it and it worked fine
so the problem is 100% in the server address...
// define some variables
$folder_path = "/Users/apple/projects/asystem";
$local_file = "auct_lots_full.xml.zip";
$server_file = "auct_lots_full.xml.zip";
//-- Connection Settings
$ftp_server = "ftp://xxxx_user:Eecohshxxxxxx#auctionsdata.xxxxx.com"; // Address of FTP server.
$ftp_user_name = "xxxxx_user"; // Username
$ftp_user_pass = "xxxx"; // Password
#$destination_file = "FILEPATH";
// 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)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
so yes! the problem was that i use wrong path to the FTP. This code works:
//-- Connection Settings
$ftp_server = "auctionsdata.xxxxx.com"; // Address of FTP server.
$ftp_user_name = "xxxxx_user"; // Username
$ftp_user_pass = "xxxx"; // Password

Getting multiple files over FTP with ftp_get and ftp_nlist

I'm attempting to get multiple files from another domain using ftp_get and ftp_nlist. ftp_nlist expects a resource and a string, but the below returns
ftp_nlist() expects parameter 1 to be resource, null given in
and
Invalid argument supplied for foreach()
<?php
// Connect and login to FTP server
$ftp_server = "hostname";
$ftp_username ="username";
$ftp_userpass = "password";
$includes = "/directory/";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
// Get file list
$contents = ftp_nlist($conn_id, $includes);
// Loop through for file 1
foreach ($contents as $file) {
$local_file = '/path/to/file.php';
$server_file = '/path/to/file.php';
ftp_get($conn_id, $local_file, $server_file, FTP_BINARY);
}
// Loop through for file 2
foreach ($contents as $file) {
$local_file = '/path/to/file.php';
$server_file = '/path/to/file.php';
ftp_get($conn_id, $local_file, $server_file, FTP_BINARY);
}
// close connection
ftp_close($ftp_conn);
?>
Variable $conn_id that you pass to ftp_nlist() is not defined. You need to pass $ftp_conn in all ftp_* functions instead. (ftp_get() in your case)
Check ftp_close() as well, to be sure you did not forget to close connection.
I'd recommend to use a wrapper for ftp_ functions, like https://github.com/dg/ftp-php, to make debugging easier. You will be able to use Exceptions and catch them like this:
try {
$ftp = new Ftp;
$ftp->connect($ftp_server);
$ftp->login($ftp_username, $ftp_userpass);
$ftp->nlist($includes);
} catch (FtpException $e) {
echo 'Error: ', $e->getMessage();
}

PHP ftp_pwd returns empty

I'm writing a php script that gets called via ajax in javascript. I'm tyring to connect to an ftp server and list all the files in the "public_html" folder.
I also wanted to print the current ftp dir but when when I try, it prints as empty in the console.
I can connect to the ftp server, but I can't tell what dir is the current one.
//Get ftp user/pass
$ftp_server = "ftp." . $_POST['hostname'];
$ftp_username = $_POST['user'];
$ftp_userpass = $_POST['pass'];
echo "console.log('attempting to connect to ftp host: $ftp_server, user: $ftp_username, password: $ftp_userpass');\n";
$ftp_conn = ftp_connect($ftp_server) or die("alert('Could not connect to $ftp_server');\n");
// turn passive mode on
//ftp_pasv($ftp_conn, true);
ftp_chdir($ftp_conn, 'public_html');
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
// then do something...
echo "console.log('Connected to FTP');\n";
// get contents of the current directory
$curdir = ftp_pwd($ftp_conn);
echo "console.log('Current dir: $curdir');\n";
$contents = ftp_nlist($ftp_conn, ".");
// output $contents
echo "var files = [];";
foreach ($contents as $file){
echo "var tempFile = '$file';\n
files.push(tempFile);\n";
}
echo "console.log(files);\n";
// close connection
ftp_close($ftp_conn);
?>
swap these two lines from:
ftp_chdir($ftp_conn, 'public_html');
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
to:
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_chdir($ftp_conn, 'public_html');
I have some problems, when using ftp_pasv();
Try to comment this line:
ftp_pasv($ftp_conn, true);

File upload through FTP in php

I want to upload a file to multiple FTP.But it is showing error :
ftp_put() [function.ftp-put]: php_connect_nonb() failed: Operation now in progress (115).
Have a look to my code
foreach ($channel_details as $channel_list)
{
if(isset($connection))
unset($connection);
if(isset($login))
unset($login);
if(isset($upload))
unset($upload);
$server = $channel_list['channel'];
$ftp_user_name = $channel_list['username'];
$ftp_user_pass = $channel_list['password'];
$source='file_push.html';
$dest='/public_html/'.$path.$file;
$connection = ftp_connect($server) or die("Couldn't connect to ftp server");
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
// turn passive mode on
ftp_pasv($connection, true);
if (!$connection || !$login) { die('Connection attempt failed!'); }
$upload = ftp_put($connection, $dest, $source, FTP_ASCII);
if (!$upload) { echo 'FTP upload failed!'; }
ftp_close($connection);
}
Your code seems to be correct.
Maybe there is a firewall problem?
See here: https://bugs.php.net/bug.php?id=47110

Categories