PHP doğru genel bir Mime türü uygulamak nasıl?

2 Cevap

Eğer görünümü tıklatın ederseniz, tarayıcı bu dosyayı açarız

Ben denedim:

readfile('test.jpg');

Ama firefox başarısız görünüyor.

2 Cevap

Eğer bir dosya için MIME türünü almak istiyorsanız, PHP, en az iki seçeneğiniz var:

İlki (artık önerilmiyor) işlevini kullanmak için mime_content_type :

Returns the content type in MIME format, like text/plain or application/octet-stream.


The second would be to use the new
Fileinfo extension (Available as a PECL extension for PHP < 5.3, and integrated in PHP >= 5.3) ; the finfo_file function seems to be the one you'll need :

Returns a textual description of the contents of the filename argument, or FALSE if an error occurred.

Ve örnek verilen (quoting):

$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
foreach (glob("*") as $filename) {
    echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);

Çıktı bu tür verir:

text/html
image/gif
application/vnd.ms-excel

Hangi tür size uygulama göndermek gerekebilir Content-type HTTP başlık için kullanmanız gerekir ne gelir ;-)

Sadece bu dosyaya bir bağlantı sağlamak ve tarayıcı dinlenme yapacak. Bu dosya sunucusunda depolanan ise muhtemelen dış dünyaya gösterecektir bir komut arıyoruz. Bu komut doğru MIME türünü ayarlamak gerekir ve daha sonra readfile hile yapmak gerekir.

<?php
header('Content-type: image/gif');
readfile('/path/to.your/file.gif');
exit();