I am using the following format in php code to download files from ftp server.
file_put_contents(
$filePath.$fileName, file_get_contents(
ftp://username:password#server_name/folder_name/xyz#123.csv.zip
)
);
I am getting an 550 error as the file name contains '#'. How to avoid the error. Also what is the best PHP class to do different operations on FTP ?
Use this
<?php
// define some variables
$local_file = 'filename.jpg';
$server_file = 'filename.jpg';
$ftp_server="www.abc.com";
$ftp_user_name="username";
$ftp_user_pass="password";
$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);
?>
true == (
$data = #
file_get_contents('ftp://username:password#server_name/folder_name/xyz#123.csv')
)
?
file_put_contents('xyz#123.csv', $data)
:
exit;
Try:
$output = exec("wget -N ftp://username#ftp.server.com/path/to directory/file 2>&1 |grep 'filename w/o extension'|grep -v ftp|grep -v =");
print $output <to check if file downloaded or not>
Related
<?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,
I am trying to upload my local file to server with the following code copied from stackoverflow. I couldnt map the files right and looking for assistance.
I got two test cases with $localfile.
mapping with relative path using c:, it throws filename prohibited.
mapping with server path. it says connected and written successfully. But in server the file size is 0KB only.
The code:
//$local_file = 'C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\sample.csv';
//Warning: ftp_put(): Prohibited file name: C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\sample.csv in C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\test.php on line 28
// The filename that i got the php code is test.ph
$local_file = 'sample.csv';
//Connected!Successfully written to sample.csv - but not written.
//The file size is showing 0KB.
$server_file = 'http://iseedtechnologies.in/sample.csv';
// set up basic connection
$conn_id = ftp_connect("iseedtechnologies.in");
// login with username and password
$login_result = ftp_login($conn_id, '****', '****');
echo is_array(ftp_nlist($conn_id, ".")) ? 'Connected!' : 'not Connected! :(';
ftp_pasv($conn_id, true);
// try to download $server_file and save to $local_file
if (ftp_put($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);
?>
Working code (Removed the http:// and it worked)
$local_file = 'sample.csv';
$server_file = 'sample.csv';
// set up basic connection
$conn_id = ftp_connect("iseedtechnologies.in");
// login with username and password
$login_result = ftp_login($conn_id, '****', '****');
echo is_array(ftp_nlist($conn_id, ".")) ? 'Connected!' : 'not Connected! :(';
ftp_pasv($conn_id, true);
// try to download $server_file and save to $local_file
if (ftp_put($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);
?>
I have two similar websites.Some of the content are same in both sites.I have some files in specific folder in one website say A.I want to copy some specific files from Website A to website B.
I have tried ftp functions in php but not working.
<?php
// define some variable
$local_file = 'eg.html';
$server_file = 'http://example.com/horoscope/M12459TAM_03092009_123239.html';
// set up basic connection
$conn_id = ftp_connect("example.com");
// login with username and password
$login_result = ftp_login($conn_id, 'username', 'password');
echo is_array(ftp_nlist($conn_id, ".")) ? 'Connected!' : 'not Connected! :(';
ftp_pasv($conn_id, true);
// 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);
?>
I get connected message but display "There was a problem".Pls can anyone try this..
Regards,
Rekha
change last part where ftp_get to ftp_put
if (ftp_put($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
$connection = ssh2_connect('IP address', Port_No);
ssh2_auth_password($connection, 'server_username', 'server_password');
ssh2_scp_send($connection,'File_Path','Destination_Path', 0644);
<?php
$local_file = "/home/zzh/Desktop/1.txt";
$server_file = "/var/www/helpdesk/1.txt";
$ftpserver = "xxx.xxx.xxx.xx";
$port = xx;
$conn_id = ftp_connect($ftpserver,$port) or die('can\'t connect to ftp');
$ftpname="xxx";
$ftppass = "xxx";
ftp_login($conn_id, $ftpname, $ftppass);
if (ftp_get($conn_id, $local_file, $server_file, FTP_ASCII)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
i want use this php to login the ftp server download 1.txt to replace the 1.txt in the desktop, but it seems doesn't work, i am using ubuntu. needs help please.
ps. i can connect to the ftp successfully.
It should replace it if IIRC.
If not, why not do
if ( file_exists($local_file) ) unlink($local_file)
This would go before your ftp_get procedure.
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);
?>
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?
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.
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);
?>