I have a folder with many files of different types. I need to display any requested file, so the header has to be adapted accordingly. What I tried is:
header( 'Content-Type: ' . filetype( 'files/' . $file ) );
readfile( 'files/' . $file );
It works for JPG, PNG and GIF files, but for some reason MP3 files already crash the browser, so I suppose it doesn’t work for every file type. Is there something wrong with the approach, or the code? How would you do it? Thanks!
By the PHP Documentation,
filetype()Returns the type of the file. Possible values are fifo, char, dir, block, link, file, socket and unknown.What you want is the MIME type of the file. What you want is Fileinfo. For example:
You can also quickly try mime_content_type, if available, just to make sure there is nothing else wrong with the system. But this is not recommended because it is depricated.