Download direct FTP file

6

How do I download a file directly from FTP without having to temporarily download 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 files very large and the large amount of users is leaving the network slow.

How can I make it authenticate to FTP through PHP and make a direct download link between user and FTP?

    
asked by anonymous 26.04.2018 / 15:50

2 answers

4

One possible solution is to use the Client URL Library

See how it works:

//Configuração de conexão com o FTP
$ftp_arquivo = "pasta/pasta/meuarquivo.txt";
$ftp_host = "199.99.854.99"; //exemplo
$ftp_port = "21";
$ftp_username = "usuarioAtenticacaoFTP";
$ftp_senha = "MinhaSenhaFTP";


header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($ftp_arquivo) . "\"");
header("Content-Transfer-Encoding: binary");

//Configura a conexão curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,  "ftp://$ftp_host:$ftp_port/$ftp_arquivo"); //Caminho do arquivo
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$ftp_username:$ftp_senha"); //Faz a autenticação
curl_exec ($ch); //Executa

curl_close($ch);

This way the file does not need to be downloaded temporarily on the PHP server.

PHP only authenticates with FTP and the file is downloaded directly to the user's browser.

    
04.05.2018 / 18:39
2

You can use the "protocol" php:// for example php://temp , like this:

 <?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 = "php://temp";

 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);

Note that by default the limit is 2MB, so if it is bigger you will have to adjust it in the settings, or you can choose to use php://output combined with ob_start , like this:

 <?php
 ob_start();

 $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 = "php://output";

 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 . "\"");

Documentation: link

    
04.05.2018 / 18:36