php 上传文件 move_uploaded_file

<?php
define('MAX_SIZE_FILE_UPLOAD','2000');//文件大小不能超过2KB
define('FILE_UPLOAD_DIR','./images/');//文件上选路径,当前目录的 images下
$array_extention_interdite=array('.gif','.jpg','.png');

$ext=ereg_replace('^[[:alnum:]]([-_.]?[[:alnum:]])*\.','.',$_FILES['file']['name']);
$submit=$_POST['submit'];
if($submit!=""){
$msg="";
$isupload=true;
if($isupload&&!in_array($ext,$array_extention_interdite)){
$isupload=false;
$msg="请选择正确的文件类型(.gif,.jpg,.png)";
}

if($isupload&&$_FILES['file']['size']>MAX_SIZE_FILE_UPLOAD){
$isupload=false;
$msg="图片大小不能超过".MAX_SIZE_FILE_UPLOAD/1000;
$msg.="kb";
}
if($isupload){
if(move_uploaded_file($_FILES['file']['tmp_name'],FILE_UPLOAD_DIR.$_FILES['file']['name'])){
$msg= "上传成功";
}else{
$msg= "检查文件路径";
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>add</title>
<script>
function check(){
var file=document.getElementById("file").value;
if(file==""){
alert("请选择图片(.gif,.jpg,.png)");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" onsubmit="return check();">
<table style="width:100%;height:100%;">
<tr>
<td>浏览本地图片</td>
<td><input type="file" name="file" id="file"></td>
</tr>
<tr>
<td><?php echo $msg?></td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="提交" id="submit"/></td>
</tr>
</table>
</form>
</body>
</html>

你可能感兴趣的:(html,PHP,XHTML,ext)