Download file directly from FTP

Asked

Viewed 1,431 times

6

How do I download a file directly from FTP without temporarily downloading to the PHP server?

inserir a descrição da imagem aqui

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?

  • 1

    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?

  • 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.

  • 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).

  • @Filipemoraes could give an example of download via token? How to implement etc...

  • @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.

  • 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 each ftp is on a different server. When the client buys system licenses and it already has an FTP, we just configure the connection.

Show 2 more comments

2 answers

4

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

 <?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 larger you will have to adjust in the settings, or you can choose to use php://output combined with ob_start, thus:

 <?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: http://php.net/manual/en/wrappers.php.php

  • Thanks for the return, but I was left with a question. The file still goes to the PHP server or goes straight to the client?

  • @Flaviaandrade goes "straight" to the output buffer that in turn is sent via download, so it is not saved in any file, this is the purpose of php://output ;) - for your case I think it best that php://temp, because temp has size limit of 2mb by default.

4


One possible solution is to use the Library Client URL

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 temporarily downloaded from the PHP.

The PHP only realizes the authentication with FTP and the file is downloaded directly to the user’s browser.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.