Problem when trying to download large files

Asked

Viewed 81 times

2

I am using this code to download with php, small files works, now big as 1Gb end up getting corrupted.

$arquivo = $_GET['nome']; //nome do Arquivo
$local = $_GET['dir']; //pasta onde está o arquivo

header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local.$arquivo));
header('Content-Disposition: filename='.$arquivo);
header("Content-Disposition: attachment; filename=".basename($local.$arquivo));

//envia o download
readfile($local.$arquivo);

<a href="download?nome=arquivo.ext&dir=pasta/">Download</a>

1 answer

2


One option is to use x-sendfile . First of all you need to have mod_xsendfile in the server module. If you don’t have it, the above link has a download area.

Then you need to configure it in your file httpd.conf adding this:

XSendFile on // <-- ligou a criança
XSendFilePath /path/to/files/directory // <-- diretório dos arquivos que estão liberados para serem acessados

And then you’ll be able to use it:

header("X-Sendfile: $arquivo"); // <--- aqui está!
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".basename($local.$arquivo));

I used only once and it worked with a very large file!

Here’s a example on Github

Browser other questions tagged

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