I have a project for a user that has a gallery of uploaded mp3 files that are stored outside of the httpdocs file tree and are being accessed via a php readfile() function. The function works, but the page itself is impossibly slow to load.
I have a php file that is being called as the src for each audio file which I have used in several applications.
$sql='SELECT `name`,`file`, `filetype`,`access` FROM `'.$DBtable.'` WHERE `id`="'.intval($_GET['file']).'";';
$database->setQuery($sql);
$file=$database->loadObject();
//Check access against my access only if the file is restricted
if($file->access>0)
{
$access=isAuth($database);
if($access<$file->access)
{
die('Restricted Access');
}
}
$uploaddir=BASE_URI;
$filename=$file->file;
switch ($file->filetype) {
case "pdf": $ctype="application/pdf"; break;
//case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
case "mp3": $ctype="audio/mpeg";break;
case "ogg": $ctype="audio/ogg"; break;
default: $ctype="application/force-download";
}
header('Content-type: '.$ctype);
header('Content-Disposition: attachment; filename="'.$file->name.'.'.$file->filetype.'"');
readfile($uploaddir.$filename);
The page itself is calling
<img data-src="file.php?source=images&file=668" class="thumbnail" parent="667">
<audio class="gallery_audio" controls="controlls">
<source src="audio_file.php?file=123" type="audio/mpeg">
<source src="audio_file.php?file=124" type="audio/ogg">
Your browser does not support the audio element.
</audio>
where file.php and audio_file.php are copies of the file loading php code. I have tried creating multiple copies of these files to spread out the http requests, thinking that maybe the server was being overloaded by repeated requests for the same file, *there are only 10, but still no love. Also it does appear that FF consistently handles this better than IE or Chrome. When I am only loading images, it seems to respond better even for much larger quantities of requests. These are very small audio files. 50-90KB so size should not be major issue.
Is there a better direction to be headed for audio files? Lazy load helps with the images, but does not seem to help much with the audio.