Reputation: 1995
I would like to use fpassthru
to relay files from another server.
The files are stored as static files on a webserver (thus the MIME-type
is set correctly by Apache
), but when I fpassthru
then via PHP
, they lose their MIME-type
.
Basically what I would like to do is to relay the file as if it came from a static server (i.e. with the same headers or at least with the same MIME-type), but I only want to use one connection to receive both data and MIME-type for performance reasons.
How can I get the MIME-type
of the file that I receive with fpassthru
(or file_get_contents
or another function)?
Upvotes: 1
Views: 1360
Reputation: 21492
Get the MIME type with mime_content_type
function, then set the Content-Type
header using the header
function, e.g.:
<?php
$path = '/path/to/local/file.png';
$mime_type = mime_content_type($path) ?: 'application/octet-stream';
$file_size = filesize($path);
header("Content-Type: $mime_type");
header("Content-Length: $file_size");
$fh = fopen($path, 'rb');
fpassthru($fh);
fclose($fh);
Upvotes: 1