简体   繁体   English

用于在不同浏览器中以PHP格式上载pdf的Mime类型

[英]Mime types for uploading pdf in PHP in different browsers

I am creating an web application that allows users to upload pdf documents. 我正在创建一个允许用户上传pdf文档的Web应用程序。 The following is the code to check(Restrict) the document type to pdf. 以下是检查(限制)文档类型为pdf的代码。

if (isset($_POST['submitbutton'])) {
  $name = $_FILES['myfile']['name'];
  $type = $_FILES['myfile']['type'];
  $size = $_FILES['myfile']['size'];
  $tmpname = $_FILES['myfile']['tmp_name'];
  $ext = substr($name, strrpos($name,'.'));  //Get the extension of the selected file
  echo $type.' This is the type';
  echo '<br>';
  if (($type == "application/pdf")||($type == "application/octet-streamn")) {
    echo 'ok';
  } else {
    echo "That is not a pdf document";
  }
}

I checked the types by echo(ing) $type to the screen. 我通过echo(ing)$ type检查了类型到屏幕。 But the type output is different for firefox. 但是firefox的类型输出是不同的。 I just need to confirm the following mime types. 我只需要确认以下mime类型。 Am I correct when I say: 我说的时候我是否正确

Internet explorer : application/pdf Internet Explorer:application / pdf

Opera: application/pdf Opera:application / pdf

FireFox : application/octet-streamn FireFox:application / octet-streamn

or is there a standard mime type that covers all existing browsers. 或者是否存在涵盖所有现有浏览器的标准mime类型。 Please help, I am still getting my feed wet with php and files 请帮助,我仍然用PHP和文件弄湿我的饲料

Don't trust $_FILES['myfile']['type'] as it is provided by the client. 不要信任$_FILES['myfile']['type']因为它是由客户提供的。 A better solution is to use finfo_open : 更好的解决方案是使用finfo_open

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, $_FILES['myfile']['tmp_name']);
if($type == 'application/pdf'){
    //Is a PDF
}

To avoid confusion, and browser issues use JQuery 为避免混淆,浏览器问题使用JQuery

var filename="";
$('#upload').on( 'change', function() {
filename= $( this ).val();
var exten = filename.split('.').pop();
if(exten!="pdf"){
    alert("Only pdf documents are allowed");
    $(this).val('');
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM