通过PHP从FTP下载文件。
我有代码:
$ftpHost = '123123';
$ftpUsername = '123123';
$ftpPassword = '123123';
$connId = ftp_connect($ftpHost) or die("Couldn't connect to $ftpHost");
if(ftp_login($connId, $ftpUsername, $ftpPassword)){
echo "Connected as $ftpUsername@$ftpHost";
}else{
echo "Couldn't connect as $ftpUsername";
}
$localFilePath = 'file.mp3';
$remoteFilePath = 'test/file.mp3';
if(ftp_get($connId, $localFilePath , $remoteFilePath, FTP_BINARY)){
// header('Content-Disposition: attachment; filename="'. $remoteFilePath .'"');
// readfile($remoteFilePath);
echo "File transfer successful - $localFilePath";
}else{
echo "There was an error while downloading $localFilePath";
}
ftp_close($connId);
脚本有效,将文件从一台服务器传输到另一台服务器。
但是我想直接从FTP下载文件而不将其保存到服务器方法ftp_get(),因为文件随后保存在服务器上。我可以使用unlink()
,但是我不想消耗服务器传输资源。