第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何從 PPT 或 docx 的 Base 64 字符串獲取 MIME-TYPE?

如何從 PPT 或 docx 的 Base 64 字符串獲取 MIME-TYPE?

PHP
紫衣仙女 2023-10-22 21:47:20
我從追加中獲取 Base64 字符串,然后用 PHP 對(duì)其進(jìn)行解碼并將其保存在數(shù)據(jù)庫中。該字符串可以是任何文件 .pdf、.img、.docx、.zip、.ppt、.docx 等。我的 base64 字符串不包含 mime 類型,例如“data:application/pdf;base64”部分。所以我需要獲取 base64 的 mime 類型。有什么辦法可以用PHP解決這個(gè)問題嗎?目前,我正在使用此代碼,它對(duì)于圖像和 pdf 工作得很好。$file = base64_decode($formData['image'], true)$mineType = $this->getMimeType($file);public function getBytesFromHexString($hexdata){    for ($count = 0; $count < strlen($hexdata); $count += 2)        $bytes[] = chr(hexdec(substr($hexdata, $count, 2)));    return implode($bytes);}public function getMimeType($imagedata){    $imagemimetypes = array(        "jpg" => "FFD8",        "png" => "89504E470D0A1A0A",        "gif" => "474946",        "bmp" => "424D",        "tiff" => "4949",        "pdf" => "25504446",        "docx"=> "504B0304",        "doc" => "D0CF11E0A1",        "xlsx"=> "504B030414000600",        "xls" => "D0CF11E0A1B11AE1"    );    foreach ($imagemimetypes as $mime => $hexbytes) {        $bytes = $this->getBytesFromHexString($hexbytes);        if (substr($imagedata, 0, strlen($bytes)) == $bytes){            return $mime;        }    }    return false;}
查看完整描述

1 回答

?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

function base64_mimetype(string $encoded, bool $strict = true): ?string {

    if ($decoded = base64_decode($encoded, $strict)) {

        $tmpFile = tmpFile();

        $tmpFilename = stream_get_meta_data($tmpFile)['uri'];


        file_put_contents($tmpFilename, $decoded);


        return mime_content_type($tmpFilename) ?: null;

    }


    return null;

}

http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types


查看完整回答
反對(duì) 回復(fù) 2023-10-22
  • 1 回答
  • 0 關(guān)注
  • 180 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)