2020年5月28日
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><form action="demo2.php" method="post" enctype="multipart/form-data"><input type="file" name="f">、<button>上传</button></form></body></html><?php$file= $_FILES['f'];var_dump($file);//判断是否有错误if($file['error'] > 0){switch($file['f']){case 1:exit('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');break;case 2:exit('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');break;case 3:exit('文件只有部分上传');break;case 4:exit('没有文件被上传');break;case 6:exit('找不到临时文件夹');break;case 7:exit('文件写入失败');break;}}//文件大小校验//定义变量保存=>允许上传最大字节数$size=1024*1024*10;if($file['size'] > $size){exit('文件大小超出限定大小');}$type =['image/png','image/jpg','image/gif',];//判断当前文件类型是否允许上传if(! in_array($file['type'],$type)){exit('文件类型不被允许'.$file['type']);}//获得md5$fileData=file_get_contents($file['tmp_name']);$fileMd5 =md5($fileData);//获得id$id=uniqid();//获得文件后缀$fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);//拼接成文件名$fileName=$fileMd5 .'-'.$id.'.'.$fileExt;//拼接完整的保存路径$saveDir='./uploads/';$savaPath=$savaDir . $fileName;$isFile =is_uploaded_file($_FILES['f']['tmp_name']);if(! $isFile){exit('is_uploaded_file 判断出非法上传');}$moveFile=move_uploaded_file($_FILES['f']['tmp_name'],$savaPath);if(! $moveFile){exit('move_uploaded_file 判断出非法上传');}echo"上传成功";?><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><h1>多文件上传</h1><form action="./demo3.php" method="post" enctype="multipart/form-data"><label for="">文件1:</label><input type="file" name="f[]"><br><label for="">文件2:</label><input type="file" name="f[]"><br><label for="">文件3:</label><input type="file" name="f[]"><br><label for="">文件4:</label><input type="file" name="f[]"><br><label for="">文件5:</label><input type="file" name="f[]"><br><button>上传</button></form></body></html><?phpecho"<pre>"var_dump($_FILES);var_dump($_FILES['f']['name']);foreach($_FILES['f']['name'] as $k => $v){$name = $_FILES['f']['name'][$k];$type= $_FILES['f']['type'][$k];$tmp_name= $_FILES['f']['tmp_name'][$k];$error= $_FILES['f']['error'][$k];$size= $_FILES['f']['size'][$k];if($file['error'] > 0){switch($file['f']){case 1:exit('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');break;case 2:exit('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');break;case 3:exit('文件只有部分上传');break;case 4:exit('没有文件被上传');break;case 6:exit('找不到临时文件夹');break;case 7:exit('文件写入失败');break;}}$size=1024*1024*10;if($file['size'] > $size){exit('文件大小超出限定大小');}$type =['image/png','image/jpg','image/gif'];//判断文件类型是否在数组中 in_arrayif(! in_array($type,$allowType)){exit('禁止上传该类型文件')}$fileData=file_get_contents($file['tmp_name']);$fileMd5 =md5($fileData);//获得id$id=uniqid();//获得文件后缀$fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);//拼接成文件名$fileName=$fileMd5 .'-'.$id.'.'.$fileExt;$savePath='./uploads/' .$fileName;//保存文件if(is_uploaded_file($tmp_name) && move_uploaded_file($tmp_name,$savaPath)){echo"文件上传成功";}else{echo"文件上传失败";}}echo"</pre>";?><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><h1>单文件上传</h1><form action="./demo4.php" method="post" enctype="multipart/form-data"><input type="file" name="f"><botton>上传</botton></form></body></html><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><h1>多文件上传</h1><form action="./demo5.php" method="post" enctype="multipart/form-data"><label for="">文件1:</label><input type="file" name="f[]"><br><label for="">文件2:</label><input type="file" name="f[]"><br><botton>上传</botton></form></body></html><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><form action="demo2.php" method="post" enctype="multipart/form-data"><input type="file" name="f">、<button>上传</button></form></body></html><?php$file= $_FILES['f'];var_dump($file);//判断是否有错误if($file['error'] > 0){switch($file['f']){case 1:exit('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');break;case 2:exit('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');break;case 3:exit('文件只有部分上传');break;case 4:exit('没有文件被上传');break;case 6:exit('找不到临时文件夹');break;case 7:exit('文件写入失败');break;}}//文件大小校验//定义变量保存=>允许上传最大字节数$size=1024*1024*10;if($file['size'] > $size){exit('文件大小超出限定大小');}$type =['image/png','image/jpg','image/gif',];//判断当前文件类型是否允许上传if(! in_array($file['type'],$type)){exit('文件类型不被允许'.$file['type']);}//获得md5$fileData=file_get_contents($file['tmp_name']);$fileMd5 =md5($fileData);//获得id$id=uniqid();//获得文件后缀$fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);//拼接成文件名$fileName=$fileMd5 .'-'.$id.'.'.$fileExt;//拼接完整的保存路径$saveDir='./uploads/';$savaPath=$savaDir . $fileName;$isFile =is_uploaded_file($_FILES['f']['tmp_name']);if(! $isFile){exit('is_uploaded_file 判断出非法上传');}$moveFile=move_uploaded_file($_FILES['f']['tmp_name'],$savaPath);if(! $moveFile){exit('move_uploaded_file 判断出非法上传');}echo"上传成功";?><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><h1>多文件上传</h1><form action="./demo3.php" method="post" enctype="multipart/form-data"><label for="">文件1:</label><input type="file" name="f[]"><br><label for="">文件2:</label><input type="file" name="f[]"><br><label for="">文件3:</label><input type="file" name="f[]"><br><label for="">文件4:</label><input type="file" name="f[]"><br><label for="">文件5:</label><input type="file" name="f[]"><br><button>上传</button></form></body></html><?phpecho"<pre>"var_dump($_FILES);var_dump($_FILES['f']['name']);foreach($_FILES['f']['name'] as $k => $v){$name = $_FILES['f']['name'][$k];$type= $_FILES['f']['type'][$k];$tmp_name= $_FILES['f']['tmp_name'][$k];$error= $_FILES['f']['error'][$k];$size= $_FILES['f']['size'][$k];if($file['error'] > 0){switch($file['f']){case 1:exit('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');break;case 2:exit('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');break;case 3:exit('文件只有部分上传');break;case 4:exit('没有文件被上传');break;case 6:exit('找不到临时文件夹');break;case 7:exit('文件写入失败');break;}}$size=1024*1024*10;if($file['size'] > $size){exit('文件大小超出限定大小');}$type =['image/png','image/jpg','image/gif'];//判断文件类型是否在数组中 in_arrayif(! in_array($type,$allowType)){exit('禁止上传该类型文件')}$fileData=file_get_contents($file['tmp_name']);$fileMd5 =md5($fileData);//获得id$id=uniqid();//获得文件后缀$fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);//拼接成文件名$fileName=$fileMd5 .'-'.$id.'.'.$fileExt;$savePath='./uploads/' .$fileName;//保存文件if(is_uploaded_file($tmp_name) && move_uploaded_file($tmp_name,$savaPath)){echo"文件上传成功";}else{echo"文件上传失败";}}echo"</pre>";?><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><h1>单文件上传</h1><form action="./demo4.php" method="post" enctype="multipart/form-data"><input type="file" name="f"><botton>上传</botton></form></body></html><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><h1>多文件上传</h1><form action="./demo5.php" method="post" enctype="multipart/form-data"><label for="">文件1:</label><input type="file" name="f[]"><br><label for="">文件2:</label><input type="file" name="f[]"><br><botton>上传</botton></form></body></html><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><form action="demo2.php" method="post" enctype="multipart/form-data"><input type="file" name="f">、<button>上传</button></form></body></html><?php$file= $_FILES['f'];var_dump($file);//判断是否有错误if($file['error'] > 0){switch($file['f']){case 1:exit('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');break;case 2:exit('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');break;case 3:exit('文件只有部分上传');break;case 4:exit('没有文件被上传');break;case 6:exit('找不到临时文件夹');break;case 7:exit('文件写入失败');break;}}//文件大小校验//定义变量保存=>允许上传最大字节数$size=1024*1024*10;if($file['size'] > $size){exit('文件大小超出限定大小');}$type =['image/png','image/jpg','image/gif',];//判断当前文件类型是否允许上传if(! in_array($file['type'],$type)){exit('文件类型不被允许'.$file['type']);}//获得md5$fileData=file_get_contents($file['tmp_name']);$fileMd5 =md5($fileData);//获得id$id=uniqid();//获得文件后缀$fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);//拼接成文件名$fileName=$fileMd5 .'-'.$id.'.'.$fileExt;//拼接完整的保存路径$saveDir='./uploads/';$savaPath=$savaDir . $fileName;$isFile =is_uploaded_file($_FILES['f']['tmp_name']);if(! $isFile){exit('is_uploaded_file 判断出非法上传');}$moveFile=move_uploaded_file($_FILES['f']['tmp_name'],$savaPath);if(! $moveFile){exit('move_uploaded_file 判断出非法上传');}echo"上传成功";?><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><h1>多文件上传</h1><form action="./demo3.php" method="post" enctype="multipart/form-data"><label for="">文件1:</label><input type="file" name="f[]"><br><label for="">文件2:</label><input type="file" name="f[]"><br><label for="">文件3:</label><input type="file" name="f[]"><br><label for="">文件4:</label><input type="file" name="f[]"><br><label for="">文件5:</label><input type="file" name="f[]"><br><button>上传</button></form></body></html><?phpecho"<pre>"var_dump($_FILES);var_dump($_FILES['f']['name']);foreach($_FILES['f']['name'] as $k => $v){$name = $_FILES['f']['name'][$k];$type= $_FILES['f']['type'][$k];$tmp_name= $_FILES['f']['tmp_name'][$k];$error= $_FILES['f']['error'][$k];$size= $_FILES['f']['size'][$k];if($file['error'] > 0){switch($file['f']){case 1:exit('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');break;case 2:exit('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');break;case 3:exit('文件只有部分上传');break;case 4:exit('没有文件被上传');break;case 6:exit('找不到临时文件夹');break;case 7:exit('文件写入失败');break;}}$size=1024*1024*10;if($file['size'] > $size){exit('文件大小超出限定大小');}$type =['image/png','image/jpg','image/gif'];//判断文件类型是否在数组中 in_arrayif(! in_array($type,$allowType)){exit('禁止上传该类型文件')}$fileData=file_get_contents($file['tmp_name']);$fileMd5 =md5($fileData);//获得id$id=uniqid();//获得文件后缀$fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);//拼接成文件名$fileName=$fileMd5 .'-'.$id.'.'.$fileExt;$savePath='./uploads/' .$fileName;//保存文件if(is_uploaded_file($tmp_name) && move_uploaded_file($tmp_name,$savaPath)){echo"文件上传成功";}else{echo"文件上传失败";}}echo"</pre>";?><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><h1>单文件上传</h1><form action="./demo4.php" method="post" enctype="multipart/form-data"><input type="file" name="f"><botton>上传</botton></form></body></html><!DOCTYPE html><html><head><meta charset="UTF-8"><title>内容</title></head><body><h1>多文件上传</h1><form action="./demo5.php" method="post" enctype="multipart/form-data"><label for="">文件1:</label><input type="file" name="f[]"><br><label for="">文件2:</label><input type="file" name="f[]"><br><botton>上传</botton></form></body></html>
笔记:
文件保存功能:is_uploaded_file(文件名):bool 判断是否通过http post 上传
move_uploaded_file(文件名):bool 判断文件是否合法上传,移动文件到新位置
数据结构
$_FILES[f][name] =>array()
$_FILES[f][type] =>array()
$_FILES[f][tmp_name] =>array()
$_FILES[f][error] =>array()
$_FILES[f][size] =>array()
单文件上传:把文件上传封装称为函数,只需要去调用函数就能够去上传文件
$file array 文件信息($_FILES[‘f’])
$savaDir string 文件保存的文件夹地址
$size int 限定上传文件的大小
$type array 限定上传文件的类型
