簡體   English   中英

從php中的文件名獲取mime類型

[英]Getting mime type from file name in php

我有以下函數可以從文件名生成 mime 類型:

    function get_mime_type($file) {
      if (function_exists('finfo_open')) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype = finfo_file($finfo, $file);
        finfo_close($finfo);
      }
      else {
        $mimetype = mime_content_type($file);
      }
      if (empty($mimetype)) $mimetype = 'application/octet-stream';
      return $mimetype;
    }

我在代碼的這一部分調用這個函數:

        $out['uploads'][] = array(
          'filename' => $fldrow['field_value'],
          'mimetype' => get_mime_type($fldrow['field_value']),
          'id'       => $fldrow['ID'],
        );

$fldrow['field_value']包含'card.pdf'

我期待'application/pdf'

我收到'application/octet-stream'

我還使用 Mode=1 嘗試了這種更精細的方法: PHP Mime 類型檢查替代方法?

Mode=1 時的結果相同,Mode=0 時為空白。

我可能在這里做錯了什么?

編輯我的解決方案基於 Dymen1 的響應,並在查看該方向的其他帖子后如下:

function get_mime_type($filename) {
    $idx = explode( '.', $filename );
    $count_explode = count($idx);
    $idx = strtolower($idx[$count_explode-1]);

    $mimet = array( 
        'txt' => 'text/plain',
        'htm' => 'text/html',
        'html' => 'text/html',
        'php' => 'text/html',
        'css' => 'text/css',
        'js' => 'application/javascript',
        'json' => 'application/json',
        'xml' => 'application/xml',
        'swf' => 'application/x-shockwave-flash',
        'flv' => 'video/x-flv',

        // images
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp',
        'ico' => 'image/vnd.microsoft.icon',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'svg' => 'image/svg+xml',
        'svgz' => 'image/svg+xml',

        // archives
        'zip' => 'application/zip',
        'rar' => 'application/x-rar-compressed',
        'exe' => 'application/x-msdownload',
        'msi' => 'application/x-msdownload',
        'cab' => 'application/vnd.ms-cab-compressed',

        // audio/video
        'mp3' => 'audio/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',

        // adobe
        'pdf' => 'application/pdf',
        'psd' => 'image/vnd.adobe.photoshop',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',

        // ms office
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'docx' => 'application/msword',
        'xlsx' => 'application/vnd.ms-excel',
        'pptx' => 'application/vnd.ms-powerpoint',


        // open office
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
    );

    if (isset( $mimet[$idx] )) {
     return $mimet[$idx];
    } else {
     return 'application/octet-stream';
    }
 }

如果您查看文檔,您會發現您沒有做錯任何事情。 但如果你做更多的研究:

https://stackoverflow.com/a/3664655/3784145

您可以看到您獲得的 mime 類型是正確的,但擴展名不需要與 mime 類型匹配,如下所述:

http://nl3.php.net/manual/en/function.mime-content-type.php#85879

因此,我將使用文件后綴來確定文件 MIME 類型。 (如第一個示例所示)

我試圖使用 mime_content_type() 和 get_mime_type() 獲取文件的 mimeType 但它不起作用。

這對我有用

$file = $request->file('FILE_NAME_IN_REQUEST');
$mimeType = $file->getClientmimeType();

*注意:函數將使用 type = file 的輸入字段接收來自 .blade 文件的 $request

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM