M
M
Murad Jelilov2014-11-08 11:09:21
PHP
Murad Jelilov, 2014-11-08 11:09:21

How to properly issue files for download through php?

Good afternoon, there is a script that gives files for download to the user

$fname=$_GET['fname'];
$prgid=$_GET['prgid'];
$f=fopen($fdown, 'r');
header("HTTP/1.1 200 OK");
header("Connection: close");
header("Content-Type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Content-Disposition: Attachment; filename=".$fname);
header("Content-Length: ".$fsize); 
while (!feof($f)) {
if (connection_aborted()) {
fclose($f);
break;
}
echo fread($f, 10000);
sleep(1);
}
fclose($f);

when downloading small files, everything goes according to plan, but if the file size is large, then the script "freezes".
tell me what's wrong or is there another way to download files through a php script.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2014-11-08
@Rsa97

Why be so perverted? Is there a readfile

B
BoneFletcher, 2014-11-08
@BoneFletcher

Several methods are described in detail here:
habrahabr.ru/post/151795
In my opinion, it is better to use modules specially designed for this:
For nginx - X-Accel-Redirect

header('X-Accel-Redirect: ' . $file);
header('Content-Disposition: attachment; filename=' . basename($file));

For apache - XSendFile
header('X-SendFile: ' . $file);
header('Content-Disposition: attachment; filename=' . basename($file));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question