File download causes site to not respond
Posted: Tue Jan 22, 2008 5:23 pm
Hi guys,
I've been working with some code I found on php.net, for downloading files, which is working mostly correctly. However, when the file download starts, the user is unable to browse any more of the site. I did test this with two separate clients - I am connected to the server with my laptop and another user is connect to the site with another machine. He is able to browse the site as normal.
The code I'm using is below. If anyone has any suggestions what might be causing this problem, that would be greatly appreciated!
Cheers,
Welly
I've been working with some code I found on php.net, for downloading files, which is working mostly correctly. However, when the file download starts, the user is unable to browse any more of the site. I did test this with two separate clients - I am connected to the server with my laptop and another user is connect to the site with another machine. He is able to browse the site as normal.
The code I'm using is below. If anyone has any suggestions what might be causing this problem, that would be greatly appreciated!
Cheers,
Welly
Code: Select all
<?php
require_once('./library/defaults.inc.php');
require_once('./library/acl.inc.php');
// The sendFile function streams the file and checks if the
// connection was aborted.
function sendFile($path, $contentType = 'application/octet-stream') {
ignore_user_abort(true);
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="' . basename($path) . "\";");
header("Content-Type: $contentType");
header("Content-Length: ".(string)(filesize($path)));
$res = array(
'status' => false,
'errors' => array(),
'readfileStatus' => null,
'aborted' => false
);
$res['readfileStatus'] = readfile($path);
if ($res['readfileStatus'] === false) {
$res['errors'][] = 'readfile failed.';
$res['status'] = false;
}
if (connection_aborted()) {
$res['errors'][] = 'Connection aborted.';
$res['aborted'] = true;
$res['status'] = false;
}
return $res;
}
$id = $_GET['id'];
$result = $db->Execute("SELECT vt.* FROM queue q INNER JOIN video_types vt ON q.video_type_id = vt.id WHERE q.id = $id");
$file_name = $result->fields['file_name'];
$res = sendFile($file_store.$file_name, 'application/octet-stream');
if ($res['status']) {
// Download succeeded
$_SESSION['downloading'] = false;
} else {
// Download failed
$_SESSION['downloading'] = false;
}
?>