Upload file Format Control confusion (Application/octet-stream not limit the bat and other format upload) problem solving

Source: Internet
Author: User
Tags rar

Allow upload type part code
$uptypes =array (//Upload file type list
' 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 '
);

Determine the File Type section
if (!in_array ($_files["Upfile" ["type"], $uptypes))
Check file types
{
echo "<font color= ' red ' > cannot upload this type of file! </font> ";
Exit
}

My intention is to allow files such as RAR upload, but when testing, bat,exe and other formats can be uploaded, I tested a bit, this kind of file is also $_files["upfile" ["type"] is ' application/octet-stream ', So I can upload it.

At present I want to allow RAR upload, but also prohibit bat other format file upload what to do?


Code to resolve:
$uptypes =array (//Upload file type list
' 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;
}
$test 1= strtolower (Getfileext ($test));
if (!in_array ($test 1, $uptypes))
Check file types
{
echo "<font color= ' red ' > cannot upload this type of file! </font> ";
Exit ();
}
Summary: The direct check file extension is used $_files["Upfile" ["name"], rather than using this $_files["Upfile" ["type"]; the type of file to limit.

Upload file Format Control confusion (Application/octet-stream not limit the bat and other format upload) problem solving

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.