Problem downloading FTP / PHP files

1

This is my code to force the download:

<?php
   ftp_get($conn, $local_file , $server_file, FTP_BINARY);
   header('Content-Disposition: attachment; filename="' . $local_file . '"');
   readfile($local_file);
   ftp_close($conn);
?>

The problem is that the file is coming empty. There is something in Apache that I need to enable because the code is identical to that of the php documentation.

    
asked by anonymous 28.10.2016 / 20:09

1 answer

2

Try using the following script

<?php

set_time_limit(0); // Define o tempo máximo de execução em 0 para as conexões lentas

$arquivoLocal = '/pasta/do/arquivo.zip'; // caminho absoluto do arquivo

// Verifica se o arquivo não existe
if (!file_exists($arquivoLocal)) {
    echo "Arquivo não encontrado.";
    exit;
}


$novoNome = 'novoNome.zip'; // Definimos o novo nome do arquivo


header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$novoNome.'"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($arquivoLocal));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');

readfile($arquivoLocal);
    
28.10.2016 / 20:34