1.用JAVASCRIPT判断文件后缀,给form加上onsubmit=”return chk()”
JavaScript代码
<script language=”javascript”>
<!–
function chk () {
Ary = form1.userfile.value.split(‘.’);
filetype=Ary[Ary.length-1];
if (filetype!=’txt’)
{
alert (‘请把文件转换成.txt格式’);
return false;
}
}
–>
</script>
2.用PHP判断文件后缀
PHP代码
if(isset($_FILES['upload'])) //***判断图象类型
if($_FILES['upload']['type']==”image/pjpeg”||$_FILES['upload']['typle']==”image/gif”)
{
$type=strcmp($_FILES['upload']['type'],”image/pjpeg”)==0?”jpg”:”gif”;
if($_FILES['upload']['size']/1024<=20&$_FILES['upload']['error']==UPLOAD_ERR_OK)//判断图象大小
{
$filename=md5(uniqid(rand())).”.”.$type;
$move=move_uploaded_file($_FILES['upload']['tmp_name'],”image/$filename”);
}
else
{
echo “error!The picture is larger than 20k”;
exit();
}
}
else
{
echo “the file type wrong!”;
exit();
}
原创文章,转载请注明: 转载自感叹忽然的部落格
本文链接地址: 用PHP、js判断文件类型