<?php
header("content-type:text/html;charset=utf-8");
$allowType=array('jpg','gif','png','jpeg');
$maxsize=5000000;
if($_POST['sub']){
if($_FILES["spic"]["error"] > 0){
switch($_FILES["pic"]["error"]) {
case 1:
echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值<br>";
break;
case 2:
echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
break;
case 3:
echo "文件只有部分被上传";
break;
case 4:
echo "没有文件被上传";
break;
default:
echo "末知错误";
}
exit;
}
$strArr=explode('.',$_FILES['spic']['name']);
$hz=$strArr[count($strArr)-1];
if(!in_array($hz,$allowType)){
exit("上传文件类型不正确.");
}
if($_FILES['spic']['size']>$maxsize){
exit("上传文件大小不能超过".$maxsize."字节.");
}
$fileName='./upload/';
if(!file_exists($fileName) || !is_writable($fileName)){
if(!@mkdir($fileName,0755)){
echo "建立上传目录失败,请重新指定上传目录";
}
}
if(is_uploaded_file($_FILES['spic']['tmp_name'])){
if(@move_uploaded_file($_FILES['spic']['tmp_name'],$fileName.$_FILES['spic']['name'])){
echo "上传文件成功";
}else{
echo "上传文件失败";
}
}else{
echo "非上传文件";
}
}