Restrict upload file type program code

Source: Internet
Author: User

Using JS

Example 1

The code is as follows Copy Code

<script>
function Check () {
var filepath=path.value
Filepath=filepath.substring (Filepath.lastindexof ('. ') +1,filepath.length)
if (filepath!= ' jpg ' && filepath!= ' gif ')
Alert ("Upload only pictures in jpg or GIF format")
}
</script>

<input type=file name=path onpropertychange= "check ()" > (only images in JPG or GIF format can be uploaded)

Example 2

The code is as follows Copy Code

<script>
function ck (obj) {if (obj.value.length>0) {
var af= "jpg,gif,png,zip,rar,txt,htm";
if (eval ("with" (Obj.value) if (!/) +af.split (","). Join ("|") + "/ig.test (substring) (LastIndexOf ('. ')

+1,length))) (1; ")) {alert ("Allowed file types:n" +AF); Obj.createtextrange (). ExecCommand (' delete ')};
}}
</script>
<form>
<input type=file name=path onpropertychange= "ck (this)"/></form>

Example 3

  code is as follows copy code

/*
  * Determine the picture type
 *
 * @param ths
 *    type= "file" JavaScript object
 * @return t Rue-meets the requirements, false-does not conform to the
 */
function Checkimgtype (ths) {
 if (Ths.value = "") {
  alert ( "Please upload pictures");
  return false;
 } else {
  if (!/. Gif|jpg|jpeg|png| Gif| Jpg| PNG) {
   alert (ths.value) $/.test ("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 just deal with it like this. Need as follows

The code is as follows Copy Code

$name =$_files[' file4 ' [' name ']; Get 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 format of your uploaded file is incorrect!!";
echo "<meta http-equiv=" Refresh content= "3;url=index.php?lmbs= file Upload" > will return to the previous page in 3 seconds ... ";
}

The above method to tell the truth can only cheat children, as long as we upload the file suffix name change may be verified through the above

With a little improvement, it's not about the file suffix name.

The code is 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 file <a href= ' wend.php ' > re-upload </a> ');

}

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
One rmvb APPLICATION/VND.RN-REALMEDIA-VBR
3GP Application/octet-stream
Application/octet-stream flv
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
Application/msaccess mdb
SQL Application/octet-stream
Num Con application/octet-stream
Log Text/plain
DAT Application/octet-stream
Application/octet-stream INI
PHP Application/octet-stream
to HTML text/html
text/html htm
TTF Application/octet-stream
fon application/octet-stream
JS Application/x-javascript
XML Text/xml
Notoginseng DLL Application/octet-stream
Application/octet-stream DLL

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.