2017-11-08 14 views
0

アップロード時にXLSXファイルの正しいMIMEタイプを検出しようとしていますが、これまでのところ何も動作していないようです。PHP XLSX間違ったMIMEタイプが検出されました

$allowed_mimes = [ 
    'pdf' => 'application/pdf', 
    'png' => 'image/png', 
    'jpg' => 'image/jpeg', 
    'xls' => 'application/vnd.ms-excel', 
    'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 
    'doc' => 'application/msword', 
    'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 
    'rtf' => 'application/rtf', 
    'odt' => 'application/vnd.oasis.opendocument.text', 
]; 
$finfo  = new finfo(FILEINFO_MIME_TYPE); 
$is_allowed = array_search($finfo->file($attachment['tmp_name']), $allowed_mimes, true); 
if (false === $is_allowed) { 
    return [ 
     'error' => true, 
     'title' => 'File format not supported', 
     'message' => 'This file format is not supported yet. Format: ' . $finfo->file($attachment['tmp_name']), 
    ]; 
} 

私は.htaccessファイルにAddTypeをすでに使用している:これはどちらか私のために動作するようには思えない

AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet .xlsx 

。私はApacheを使用しているVPS(Ubuntu)上で、/etc/mime.typesファイルとapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentが存在することを確認しました。また/usr/share/mime/application/vnd.openxmlformats-officedocument.wordprocessingml.document.xmlファイルが存在します。私は問題が何であるか分かりません。

答えて

0

なぜ使用しないでくださいmime_content_type? フルパスで正しいファイル名を指定する必要があることに注意してください。 このコードは問題なく動作します:

$allowed_mimes = [ 
    'pdf' => 'application/pdf', 
    'png' => 'image/png', 
    'jpg' => 'image/jpeg', 
    'xls' => 'application/vnd.ms-excel', 
    'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 
    'doc' => 'application/msword', 
    'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 
    'rtf' => 'application/rtf', 
    'odt' => 'application/vnd.oasis.opendocument.text', 
]; 
$is_allowed = array_search(mime_content_type($attachment['tmp_name']), $allowed_mimes, true); 
if (!$is_allowed) { 
    return [ 
     'error' => true, 
     'title' => 'File format not supported', 
     'message' => 'This file format is not supported yet. Format: ' . mime_content_type($attachment['tmp_name']), 
    ]; 
} 
関連する問題