Limit upload file Type program code _php tutorial

Source: Internet
Author: User
Tags bmp image
We generally do not restrict the user to upload files when the file class, because there is no good way to restrict the use of such as php,asp to operate, I would like to introduce the use of JS to define type=file Browse upload file type and PHP limit upload file type code.

Using JS

Example 1

The code is as follows Copy Code

(only images in JPG or GIF format can be uploaded)

Example 2

The code is as follows Copy Code


Example 3

The code is as follows Copy Code

/*
* Determine the type of picture
*
* @param ths
* type= JavaScript object with "file"
* @return true-Meet the requirements, false-does not meet
*/
function Checkimgtype (ths) {
if (Ths.value = = "") {
Alert ("Please upload image");
return false;
} else {
if (!/. ( Gif|jpg|jpeg|png| Gif| Jpg| PNG) $/.test (Ths.value)) {
Alert ("The picture type must be one of the. gif,jpeg,jpg,png");
Ths.value = "";
return false;
}
}
return true;
}

If we're using the php,asp class, we can't handle it like this.

The code is as follows Copy Code

$name =$_files[' file4 ' [' name ']; Gets the name of the client machine's original file
$type =strstr ($name, "."); Gets the from "." To the last character if ($type! = ". txt")
{echo "Sorry, the file you uploaded is not in the correct format!");
echo " will return to the front page after 3 seconds ...";
}

The above method to tell the truth can only deceive the children, as long as we have to change the name of the upload file suffix can be verified by the above

After a little improvement, this is irrelevant to the file suffix.

code as follows copy code

$temppath = $upfile [' Tmp_name '];
$fileinfo =pathinfo ($upfile [' name ']);
$extension = $upfile [' type '];
Switch ($extension)
{
Case ' Application/msword ':
$extension = ' Doc ';
Break
Case ' application/vnd.ms-excel ':
$extension = ' xls ';
Break
Case ' application/vnd.openxmlformats-officedocument.wordprocessingml.document ':
$extension = ' docx ';
Break
Case ' Application/vnd.ms-powerpoint ':
$extension = ' ppt ';
Break
Case ' application/pdf ':
$extension = ' pdf ';
Break
Case ' Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ':
$extension = ' xlsx ';
Break
Default
Die (' Only allow uploading doc,docx,xls,pdf,ppt files to re-upload ');

}

ID suffix name PHP recognized file type
0 gif image/gif
1 jpg image/jpeg
2 PNG Image/png
3 BMP Image/bmp
4 PSD Application/octet-stream
5 ico Image/x-icon
6 rar Application/octet-stream
7 Zip Application/zip
8 7z Application/octet-stream
9 exe Application/octet-stream
Ten AVI Video/avi
RMVB APPLICATION/VND.RN-REALMEDIA-VBR
3GP Application/octet-stream
FLV Application/octet-stream
MP3 Audio/mpeg
WAV Audio/wav
KRC Application/octet-stream
+ LRC Application/octet-stream
TXT Text/plain
Doc Application/msword
XLS application/vnd.ms-excel
PPT Application/vnd.ms-powerpoint
PDF Application/pdf
Application/octet-stream CHM
MDB application/msaccess
SQL Application/octet-stream
Application/octet-stream Con
Log Text/plain
DAT Application/octet-stream
Application/octet-stream INI
PHP Application/octet-stream
HTML text/html
HTM text/html
Application/octet-stream TTF
Application/octet-stream fon
JS Application/x-javascript
XML Text/xml
PNS DLL Application/octet-stream
Application/octet-stream DLL


http://www.bkjia.com/PHPjc/444631.html www.bkjia.com true http://www.bkjia.com/PHPjc/444631.html techarticle we generally do not restrict the user to upload files when the file class, because there is no good way to restrict the use of such as the php,asp to operate, I would like to introduce the use of JS to define ...

  • Related Article

    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.