使用 PHP 通过 FTP 上传文件
2022-08-30 16:51:02
我很好奇如何使用PHP通过FTP上传文件。假设我有上传表单,用户上传了一个文件。如何使用PHP将文件(不从临时目录移动到)到某个FTP主机?
我很好奇如何使用PHP通过FTP上传文件。假设我有上传表单,用户上传了一个文件。如何使用PHP将文件(不从临时目录移动到)到某个FTP主机?
给你:
$ftp = ftp_connect($host, $port, $timeout);
ftp_login($ftp, $user, $pass);
$ret = ftp_nb_put($ftp, $dest_file, $source_file, FTP_BINARY, FTP_AUTORESUME);
while (FTP_MOREDATA == $ret)
{
// display progress bar, or something
$ret = ftp_nb_continue($ftp);
}
// all done :-)
为简洁起见,省略了错误处理。
请注意:您必须已安装并启用。ext-ftp
下面是一个代码示例
$ftp_server="";
$ftp_user_name="";
$ftp_user_pass="";
$file = "";//tobe uploaded
$remote_file = "";
// 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";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);