图片上传
Index.php文件代码:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title> </head> <body><form action="upload2.php" method="post" enctype="multipart/form-data"><input type="file" name='file'><input type="submit" value="上传"></form> </body> </html>
upload.php代码:
图片上传步骤:
1:接收参数
2:判断错误
3:判断格式是否合法
4:判断文件大小
5:判断是是不是真正的图片
6:判断是否是http post提交
<?php include('../function.php');//接受参数$file=$_FILES['file'];$name=$file['name'];$type=$file['type'];$tmp_name=$file['tmp_name'];$error=$file['error'];$size=$file['size'];$path='../images/';//判断错误if($error==UPLOAD_ERR_OK){// exit('上传成功');//判断格式是否合法$format=array('jpeg','jpg','png','gif');$ext=format($name);if(!in_array($ext,$format)){exit('图片格式不正确');}//判断文件大小$allowSize=1048576; //1Mif($size>$allowSize){exit('图片过大');}//判断是不是图片$imgSize=getimagesize($tmp_name);if(!$imgSize){exit('这不是一个图片');}//判断是不是通过http post上传if(is_uploaded_file($tmp_name)){if(!file_exists($path)){mkdir($path,0777,true);chmod($path,0777);}$newName = getUniqidName().".".$Ext;$dstpath = $path.'/'.$newName;if(move_uploaded_file($tmp_name,$dstpath)){echo $newName;}else{exit('图片上传失败');}}else{exit('不是HTTP POST方式提交');}}else{switch ($error) {case '1':exit('文件大小超过限定值');break;case '2':exit('文件大小超过了表单配置大小');break;case '3':exit('文件只有部分被上传');break;case '4':exit('没有文件被上传');break;case '6':exit('没有找到缓存目录');break;case '7':exit('目录不可写');break;case '8':exit('PHP扩展程序阻止了文件上传');break;}}//取文件后缀函数function format($name){$ext=explode('.',$name);$val=end($ext);return $val;}function getUniqidName(){return md5(uniqid(microtime(true),true));}?>
文件上传
Index.php文件代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="upload2.php" method="post" enctype="multipart/form-data">
<input type="file" name='file'>
<input type="submit" value="上传">
</form>
</body>
</html>
upload.php代码:
图片上传步骤:
1:接收参数
2:判断错误
3:判断格式是否合法
4:判断文件大小
5:判断是是不是真正的图片
6:判断是否是http post提交
代码:
<?php
include('../function.php');
//接受参数
$file=$_FILES['file'];
$name=$file['name'];
$type=$file['type'];
$tmp_name=$file['tmp_name'];
$error=$file['error'];
$size=$file['size'];
$path='../images/';
//判断错误
if($error==UPLOAD_ERR_OK){
// exit('上传成功');
//判断格式是否合法
$format=array('jpeg','jpg','png','gif');
$ext=format($name);
if(!in_array($ext,$format)){
exit('图片格式不正确');
}
//判断文件大小
$allowSize=1048576; //1M
if($size>$allowSize){
exit('图片过大');
}
//判断是不是图片
$imgSize=getimagesize($tmp_name);
if(!$imgSize){
exit('这不是一个图片');
}
//判断是不是通过http post上传
if(is_uploaded_file($tmp_name)){
if(!file_exists($path)){
mkdir($path,0777,true);
chmod($path,0777);
}
$newName = getUniqidName().".".$Ext;
$dstpath = $path.'/'.$newName;
if(move_uploaded_file($tmp_name,$dstpath)){
echo $newName;
}else{
exit('图片上传失败');
}
}else{
exit('不是HTTP POST方式提交');
}
}else{
switch ($error) {
case '1':
exit('文件大小超过限定值');
break;
case '2':
exit('文件大小超过了表单配置大小');
break;
case '3':
exit('文件只有部分被上传');
break;
case '4':
exit('没有文件被上传');
break;
case '6':
exit('没有找到缓存目录');
break;
case '7':
exit('目录不可写');
break;
case '8':
exit('PHP扩展程序阻止了文件上传');
break;
}
}
//取文件后缀函数
function format($name){
$ext=explode('.',$name);
$val=end($ext);
return $val;
}
function getUniqidName(){
return md5(uniqid(microtime(true),true));
}
?>