我试图在我的网页上制作一个下载按钮,使用FTP。但是当我尝试这样做的时候,下载的文件会保存到我网站的主机上,而不是我想要的用户PC上。难道不可能这样做,这样就可以像在下面的图像中一样从FTP下载文件吗?

这是我现在拥有的下载FTP功能,它将文件下载到我的网站项目文件夹中。
function FTP_download($id){
ob_end_flush();
$remote_file = $id.'.log';
$local_file = $id.'.log';
$conn_id = ftp_connect($this->ftp_server);
$login_result = ftp_login($conn_id, $this->ftp_user_name, $this->ftp_user_pass);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $remote_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
}发布于 2021-12-23 14:37:11
所以我做了一个下载我的.log文件的解决方案
FTP_download.php
...
$file = $_GET['file'].'.log';
// 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);
// get the size of $file
$size = ftp_size($conn_id, $file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . $size);
readfile('ftp://'.$ftp_user_name.':'.urlencode($ftp_user_pass).'@'.$ftp_server.'/'.$file);
exit;我有一个函数,它有到这个.php页面的下载链接。
<?php
function FTP_download($id){
?> <a href="ftp_download.php?file=<?php echo $id; ?>" download> Download logfile</a> <?php
}
?>https://stackoverflow.com/questions/70463224
复制相似问题