如何使用cron作业将文件上传到FTP

时间:2013-07-21 17:14:38

标签: php mysql cron crontab

我希望能够每天一次(从我的电脑本地)上传一个csv文件到ftp。 然后,我将把这个csv文件插入到mysql表中。

我已经创建了cron作业来获取csv并将其插入到数据库中,但我正在努力弄清楚如何获取我的loacl pc上的文件并将其上传到FTP。 / p>

有没有人有任何想法?

由于 阿迪

2 个答案:

答案 0 :(得分:2)

您可以在PHP中使用ftp扩展名,例如:

$conn = ftp_connect("destination.host", 21) or die("failed to connect");
ftp_login($conn, $user, $pass) or die("failed to login");
ftp_put($conn, "/path/on/ftp/server", "/path/on/your/local", FTP_BINARY) or die("failed to upload);

更多详情:http://us2.php.net/manual/en/book.ftp.php

答案 1 :(得分:0)

// open some file for reading

$file = 'somefile.txt';

$fp = fopen($file, 'r');

$ftp_user_name="xxxxxx";

$ftp_user_pass="xxxxx";

// set up basic connection

//$conn_id = ftp_connect($ftp_server);

$conn_id = ftp_connect("xxxxxxx.com", 21) or die("failed to connect");

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to upload $file

if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {

    echo "Successfully uploaded $file\n";
} else {

    echo "There was a problem while uploading $file\n";
}

// close the connection and the file handler

ftp_close($conn_id);

fclose($fp);