6
How do I download a file directly from FTP without temporarily downloading to the PHP server?
Code:
<?php
//....
$fcon = ftp_connect($ftp_host);
$conecta = ftp_login($fcon, $ftp_username, $ftp_senha);
ftp_pasv($fcon, true);
ftp_chdir($fcon , $ftp_pasta);
$arquivo = basename($arquivo_ftp);
$arquivo_temp = "../anexos/" . $arquivo;
ftp_get($fcon, $arquivo_temp, $arquivo, FTP_BINARY);
ftp_close($fcon);
//Prepara o arquivo para download no navegador
$file = $arquivo;
//Vê a extensão do arquivo
$type = filetype($arquivo_temp);
//Vê o tamanho do arquivo
$size = filesize($arquivo_temp);
//Seta o header da página para forçar o navegador a fazer download
header("Content-Description: File Transfer");
header("Content-Type:{$type}");
header("Content-Length:{$size}");
header("Content-Disposition: attachment; filename=\"" . $file . "\"");
//Faz download
readfile($arquivo_temp);
//Apaga o arquivo temporário
unlink($arquivo_temp);
As it is in the above code, the file needs to be fetched from FTP and temporarily downloaded to a server folder, and then sent to the user. With this very large files and the large amount of users is slowing down the network.
How can I get authentication to FTP through PHP and make a direct download link between user and FTP?
If files are part of the process and need to be on the same server, why not use the idle hours to transfer FTP content to the server and only allow the download when the file is available on the same server? Or why not implement a download system (via token, for example) on the same server where the files are and the download takes place from that server?
– Filipe Moraes
php will always run on the server side, so either you redirect the user to the file on the ftp server with a direct link (if you have no problem with the link being able to download it) or do as @Filipemoraes said and keep the data on the same server so that copy is not needed or use a token download system.
– Fernando
A copy on the server is impossible. This application server has N clients. Each client has its own FTP. Thus, at least the application has about 400 customers (companies).
– Flavia Andrade
@Filipemoraes could give an example of download via token? How to implement etc...
– Flavia Andrade
@Leocaracciolo the request would be from the user to ftp, but as ftp is authenticated do not know how to do. Hence the best explanation I created this image.
– Flavia Andrade
Are web and ftp servers in the same network section? If yes, see the possibility of mapping the FTP directory on the Web server, removing the need to copy the data, but there would be a local mapping. Implementing download via token will not resolve the issue of FTP being authenticated.
– Fernando
@Fernando each ftp is on a different server. When the client buys system licenses and it already has an FTP, we just configure the connection.
– Flavia Andrade