我希望能够每天一次(从我的电脑本地)上传一个 csv 文件到 ftp。然后我将把这个 csv 文件插入到一个 mysql 表中。
我已经创建了 cron 作业来获取 csv 并将其插入数据库,但我正在努力弄清楚如何获取我的 loacl pc 上的文件并将其上传到 FTP。
有没有人有任何想法?
谢谢阿迪
您可以在 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);
// 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);