<?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.
Related
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);
?>
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 need to write a PHP script that downloads a file (master.csv) from an ftp server (ftp.example.com) and prepares it for processing and uploading. I've tried this a number of times but my code is no good, I'm new to PHP. How would I go about doing this?
<?php
///vars
$local_file = 'xmitpart2.csv';
$server_file = 'master.csv';
$ftp_server="ftp.example.com";
$ftp_user="username";
$ftp_pass="paswd";
//connect to server
$conn_id = ftp_connect($ftp_server);
// login to ftp
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
//download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_ASCII))
{
echo "Holy Crap Finally!\n";
}
else
{
echo "Of Course.\n";
//WRONGO DIE DIE DIE
die;
}
// close the connection
ftp_close($conn_id);
You can use ftp_get();
It allows you to get a file from an FTP server and save it locally.
Once you have it saved locally you can do what ever you want to "prepare it" as you say.
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);
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>