Problem with downloading FTP/PHP files

Asked

Viewed 260 times

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 the php documentation.

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

Browser other questions tagged

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