I have media files like mp4, ogg etc to server on page. But the problem is due to some security reasons files don't have any extensions although they are media files. So Apache cant recognize the mime type and hence html5 video tags fail to play videos.
What are my options ?
since i don't have any extension, .htaccess
doesnot work. If I put file-extension in names then it starts working. Also I have mime type of these files stored in databse.
File strucure:
asdsad/media
werewr/media
23frwe/media
//where media is the file ( could be mp4/ogg/webm) with no extension.
// before / is folder name
// If i change media to media.mp4 , it works
.htaccess file
AddType video/ogg .ogv .ogm .ogg
AddType audio/ogg .oga .ogg
AddType video/mp4 .mp4
AddType video/webm .webm
AddType video/x-m4v .m4v
AddType text/x-vcard .vcf
what if I output file from php instead of linking it from localpath ? will it work ? something like this.
header("Content-type: video/mp4");
echo file_get_contents($pathTmp4);
<?php
// tell the browser the file type
header("Content-type: video/mp4");
// tell the browser the file size
header(sprint("Content-size: %d", filesize($pathTmp4)));
// disable output buffering before delivering the file
while(ob_get_level()) ob_end_clean();
// send it over
readfile($pathTmp4);
// done, track speed and such optional stuff here
?>
Consider readfile
. Will output directly bypassing the load in memory that file_get_contents
does.
Or just open the file and use fread
to send chunks. NEVER load the entire thing in memory before sending it.
UPDATE
In terms of speed, you need to test. This will bypass .htaccess issues as it uses PHP to spit out the file. This allows you to control headers and feed the appropriate types to the browser. Downside is the PHP overhead for just delivering a file.
I'd personally go for fopen
+ fread
+ fclose
combo as we're talking video files which are large. This also allows you to track sending progress in a DB, track speed of delivery and such. Download tracking in real-time might be of use to you.
But for anything else (smaller potatoes), I use readfile
(). It will not give you updates until it's done. But it does its job.
You still need to test. Use a video file of 100+ MB and send it. See how memory is doing on the PHP instance and how it behaves overall.