4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

なぜfirefoxで$_FILES['userfile']['type'] のzip判定に躓いていたのか

Last updated at Posted at 2015-06-30

アップロードファイルのMIME-Type問題。
実際引っかかったのは初めて。
網羅は当然大変なので、自分が引っかかった箇所のみ

概要

  • firefoxからのzipファイルアップロードが弾かれていた
  • IEを使うか、拡張子を消すことで回避していたらしい
  • 原因はPHPのzipファイル判定
  • MIME-Typeがfirefoxのみapplication/x-zip
  • ブラウザによって扱いが違うから注意
  • そもそも、書き換え可能な箇所なので鵜呑みにするな

:dog2: きょうの嫌コ(ード)


if (  !eregi("x-zip-compressed"	, $_FILES['userfile']['type'])
   && !eregi("octet-stream"		, $_FILES['userfile']['type'])
   && !eregi("force-download"	, $_FILES['userfile']['type'])
{
   $echo = "zipファイルではありません。";
}

ereg使っている
if文わかりづらい→ド・モルガンの法則1
firefoxからのアップロードがzip扱いされない

検証

コード

form.html

<!DOCTYPE html>
<html>
  <body>
    <form enctype="multipart/form-data" action='post.php' method='post'>
      <input type='file' name='userfile'/>
      <input type='submit' />
    </form>
  </body>
</html>

post.php

<?php
var_dump($_FILES['userfile']['type']);

結果

ブラウザ zip 拡張子削り
IE 11 application/x-zip-compressed application/x-zip-compressed
Chrome 43 application/x-zip-compressed application/octet-stream
firefox 38 application/x-zip application/octet-stream
Win10 build 10130 Spartan 152 application/x-zip-compressed application/x-zip-compressed

仮にIEでzipファイルの判定に失敗していたら、「拡張子消せば通る」という迂回策も失敗していたわけか…

結論

!eregi("x-zip" , $_FILES['userfile']['type'])
足せばいいんじゃないでしょうか
!eregi("x-zip-compressed" , $_FILES['userfile']['type'])
も内包しちゃうと思いますが

  1. &&よりも||が楽。!が少ないほうが楽。…と思っている

  2. 今日のbuild 10158 で正式にEdgeになったそうです

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?