简体   繁体   English

cronjob通过FTP将文件复制到我们的服务器

[英]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? cron作业是否可以从远程服务器复制文件,然后将其移动到我们的服务器,并覆盖当前位于该服务器上的文件?

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. 将包含两组ftp详细信息。

This is for a product feed and i really cant get my head around it. 这是针对产品Feed的,我真的无法理解。

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...). 使Cronjobs定期执行脚本(每小时,每天等)。

So everything you can do with a script, you can do with cronjobs. 因此,您可以使用脚本进行的所有操作都可以使用cronjobs进行。

after playing around with the code i noticed i was using ftp_put and not ftp_get 在玩完代码后,我注意到我正在使用ftp_put而不是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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM