上傳檔案格式控制的困惑(application/octet-stream 限制不了BAT等格式上傳)問題解決

來源:互聯網
上載者:User

標籤:

允許上傳類型部分代碼
$uptypes=array(  //上傳檔案類型列表
‘image/gif‘, ‘image/jpg‘, ‘image/jpeg‘, ‘image/pjpeg‘, ‘image/png‘,‘application/msword‘,‘application/vnd.ms-excel‘,
‘image/bmp‘,‘text/plain‘,‘application/octet-stream‘,‘image/x-png‘,‘application/x-shockwave-flash‘,‘audio/mpeg‘,‘audio/x-ms-wma‘,
‘application/x-zip-compressed‘
);

判斷檔案類型部分
if(!in_array($_FILES["upfile"]["type"], $uptypes))
//檢查檔案類型
{
echo "<font color=‘red‘>不能上傳此類型檔案!</font>";
exit;
}

      我的本意是想允許RAR這類檔案上傳,但做測試時,BAT,EXE等格式都可以上傳,我測試了一下,這類檔案的也是$_FILES["upfile"]["type"]是‘application/octet-stream‘,所以就能上傳了。

   目前我要想允許RAR上傳,又能禁止BAT其它的格式檔案上傳怎麼辦?


解決的代碼:
$uptypes=array(  //上傳檔案類型列表
‘gif‘, ‘jpg‘,‘jpeg‘,‘png‘,‘doc‘,‘xls‘,
‘bmp‘,‘txt‘,‘rar‘,‘swf‘,‘mp3‘,‘wma‘,
‘zip‘
);
$test=$_FILES["upfile"]["name"];
function getFileExt($file_name)
{
        while($dot = strpos($file_name, "."))
         {
                $file_name = substr($file_name, $dot+1);
         }
        return $file_name;
}
$test1= strtolower(getFileExt($test));
if(!in_array($test1, $uptypes))
//檢查檔案類型
{
echo "<font color=‘red‘>不能上傳此類型檔案!</font>";
exit();
}
小結:直接檢查檔案的副檔名用$_FILES["upfile"]["name"];,而不是用這個$_FILES["upfile"]["type"];檔案的類型來限制。

上傳檔案格式控制的困惑(application/octet-stream 限制不了BAT等格式上傳)問題解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.