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);
?>
Related
In my php code below, i am trying to upload a file. I have adapted this code from one of the posts on the subject. The file i have to upload has a serial number as part of the name, which is incremented everyday. So I use the glob function to put the file name in an array and loop through the array. The script works but the file created on the server is empty. I have seen a response to my question, which is to use
ftp_pasv($conn_id, true);
But I do have this in the script, it does not work for me. I hope someone can assist me locate what the problem is.
I am working in ubuntu 18.04, also connecting to a unix machine.
As an extra question, how would I delete the local file after transfer.
Thanks for understanding.
The code is below.
#!/usr/bin/php
<?php
//define some variables
$ftp_server='server-address';
//set up basic connection
$conn_id = ftp_connect($ftp_server);
$ftp_user_name="myuser";
$ftp_user_pass="mypasswd";
$remote_path = "/";
$local_path="./";
chdir($local_path);
$local_file = glob("$local_path/OBS*");
$arrlength=count($local_file);
$cur_dir= getcwd();
echo $cur_dir . "\n";
//exit;
//login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
print_r(ftp_nlist($conn_id,"."));
for($x=0;$x<$arrlength;$x++){
//upload $local_file and save to $remote_path
if(ftp_put($conn_id, "$local_path/$local_file[$x]", $remote_path, FTP_ASCII)){
echo "Successfully written to $remote_path)\n";
}
else {
echo "There was a problem\n";
}
}
//close the connection
ftp_close($conn_id);
?>
try like this
if(ftp_put($conn_id, "$local_path/$local_file[$x]", $remote_path, FTP_BINARY)){
echo "Successfully written to $remote_path)\n";
}
<?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 have searched a lot for the answer and found some results but none of one is working for me. I want to transfer zip file from one server to another server using ftp functions. File went transferred but without data. Here is my code as follows:
<?php
$file = 'LICENSE.zip';
$fp = 'LICENSE.zip';
$ftp_server = "ftpserver name";
$ftp_user_name = "ftp username";
$ftp_user_pass = "ftp password";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// login with username and password
// upload a file
ftp_chdir($conn_id,'/dev');
if (ftp_put($conn_id, $file, $fp, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
fclose($fp);
?>
Even I am unable to transfer a simple .php or .txt file with their data. In this case only files without data is moving.
Is it possible for a cron job to copy a file from a remote server and move it to our server and over-right the file currently sitting there?
I have looked for an answer on here and not found a suitable solution as most are moving a file to and not from a remote server
there will be two sets of ftp details to include.
This is for a product feed and i really cant get my head around it.
Am i on the right line of thinking with this that i have adapted.
<?php
$file = 'remotefile.txt';
$remote_file = 'ourfile.txt';
$ftp_server ='example.com';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
Yes, it's possible.
Cronjobs are made to execute scripts periodically (every hours, every day, etc...).
So everything you can do with a script, you can do with cronjobs.
after playing around with the code i noticed i was using ftp_put and not ftp_get
here is the working code
<?php
$file = 'remotefile.txt';
$remote_file = 'ourfile.txt';
$ftp_server ='example.com';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
// 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);
// upload a file
if (ftp_get($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
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>