Rick Nash
Rick Nash

Reputation: 57

cronjob to copy a file to our server via FTP

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);
?>

Upvotes: 2

Views: 2499

Answers (2)

Rick Nash
Rick Nash

Reputation: 57

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);
?>

Upvotes: 2

Dr. Z
Dr. Z

Reputation: 237

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.

Upvotes: 3

Related Questions