作者:Wi俏娃寒躯 | 来源:互联网 | 2018-03-19 16:57
图片上传
Index.php 文件代码:
DOCTYPE html >
< html lang ="en" >
< head >
< meta charset ="UTF-8" >
< title > Documenttitle >
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 ));
}
?>
文件上传
Index.php 文件代码:
上传 ">
upload.php 代码:
图片上传步骤:
1 :接收参数
2 :判断错误
3 :判断格式是否合法
4 :判断文件大小
5 :判断是是不是真正的图片
6 :判断是否是 http post 提交
代码 :
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));
}
?>