PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Technocrat
Forum Contributor
Posts: 127 Joined: Thu Oct 20, 2005 7:01 pm
Post
by Technocrat » Fri Jan 06, 2006 3:21 pm
With the help of a few posts around here I figured out how to send a download to a user. The problem is there is a delay of around 30 seconds between when someone clicks and when the download start.
Its not the server because the download starts right away if I send the file link via header. So I am not sure what the problem is.
Any help would be great, thanks.
Code: Select all
if (!function_exists('mime_content_type')) {
function mime_content_type($f) {
$f = escapeshellarg($f);
return trim( `file -bi $f` );
}
}
$file = $folder . '/' . $file_name;
$size = filesize($file);
$content = mime_content_type($file);
$name = basename($file);
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private");
header("Content-length: $size");
header("Content-type: $content");
header("Content-Disposition: attachment; filename=$name");
readfile($file);
Last edited by
Technocrat on Fri Jan 06, 2006 6:00 pm, edited 1 time in total.
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Fri Jan 06, 2006 3:40 pm
probably the function mime_content_type does not exists, and forking a process for "file -bi $file" might take a while... It is the reason why people moved from cgi-bin to apache modules...
possible solution: recompile php --with-mime-thingies...
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Fri Jan 06, 2006 3:46 pm
from the comments on php.net:
sunfra at firenze dot net
29-Apr-2005 11:31
if you arern't allowed to use mime_content_type, you can use this function
Code: Select all
<?php
function mime_content_type_($filename)
{
$mime = array(
'.3dmf' => 'x-world/x-3dmf',
'.a' => 'application/octet-stream',
'.aab' => 'application/x-authorware-bin',
//[... i have to cut, the text is too long :/ ..]
'.xwd' => 'image/x-xwd',
'.xyz' => 'chemical/x-pdb',
'.z' => 'application/x-compressed',
'.zip' => 'application/x-zip-compressed',
'.zoo' => 'application/octet-stream',
'.zsh' => 'text/x-script.zsh',
);
return $mime[strrchr($filename, '.')];
}
?>
Technocrat
Forum Contributor
Posts: 127 Joined: Thu Oct 20, 2005 7:01 pm
Post
by Technocrat » Fri Jan 06, 2006 4:40 pm
timvw - That would make sense but I dont think my host is going to allow me to do this, much less can I ask users to do this.
Jenk - I saw that too, but its chopped off. I am going to have to find a whole copy of it some where.
Technocrat
Forum Contributor
Posts: 127 Joined: Thu Oct 20, 2005 7:01 pm
Post
by Technocrat » Fri Jan 06, 2006 4:57 pm
Still doing it though a BIT faster
I have narrowed it down to:
header("Content-length: $size");
LiveFree
Forum Contributor
Posts: 258 Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town
Post
by LiveFree » Fri Jan 06, 2006 5:21 pm
w00t its Technocrat
Technocrat
Forum Contributor
Posts: 127 Joined: Thu Oct 20, 2005 7:01 pm
Post
by Technocrat » Fri Jan 06, 2006 6:02 pm
GZip and the Content-length: dont like each other so to fix it I had to do:
Code: Select all
while (ob_end_clean());
header('Content-Encoding: none');