Php 为浏览器设置文件的mime类型
我正在使用函数fopen和fwrite创建一个文件,以便在其中写入内容 我正在写的数据是二进制的(从pdf文件加密)。一旦数据写入,我会检查macbook中的新文件类型,它显示pdf,但当我在服务器中运行Php 为浏览器设置文件的mime类型,php,pdf,mime-types,Php,Pdf,Mime Types,我正在使用函数fopen和fwrite创建一个文件,以便在其中写入内容 我正在写的数据是二进制的(从pdf文件加密)。一旦数据写入,我会检查macbook中的新文件类型,它显示pdf,但当我在服务器中运行mime\u content\u type时,输出为text/plain 所创建文件的扩展名为pdf 代码: 您知道如何将mime内容类型设置为application/pdf 谢谢检查功能 我知道header函数,我在问,当我在文件上使用mime\u content\u type时,它是否可能会
mime\u content\u type
时,输出为text/plain
所创建文件的扩展名为pdf
代码:
您知道如何将mime内容类型设置为application/pdf
谢谢检查功能
我知道header函数,我在问,当我在文件上使用mime\u content\u type时,它是否可能会显示application/pdf而不是text/plain如果你像这个答案中那样正确设置HTTP头,它应该输出正确的mime类型。您是否明确设置了它?您需要发布代码,其中可能有失败和/或丢失的内容。您是否也尝试过finfo_file()
?mime类型为FILEINFO\u mime\u类型。您还应该尝试在服务器上直接检测命令行中的类型。ie#file something.pdf
顺便说一句。对于mime类型检测,扩展名一点也不重要(也不应该如此)。@Fred ii-代码发布。
$fileurl = $targetFolder . '/' . $id . '.' . $filetype;
$blockCipher = BlockCipher::factory('openssl', array('algo' => 'aes'));
$blockCipher->setKey('test');
$create_file = fopen('./public' . $fileurl, 'w');
$file_content = file_get_contents($files[$fileKey]['tmp_name']);
$encrypted_file = $blockCipher->encrypt($file_content);
if( fwrite($create_file, $encrypted_file) ) {
chmod('./public' . $fileurl, 0644);
}
fclose($create_file);
<?php
// We'll be outputting a PDF
header('Content-Type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>