<form action="3-upload.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="300000000000000" /> 片名:<input type="text" name="name" /><p /> 请上传大片:<input type="file" name="av" /><p /> <input type="submit" /> </form>
pHp:
<?php /*echo "<pre>"; var_dump($_POST); echo "</pre>";*/ //1.观察数组 /*echo "<pre>"; var_dump($_FILES); echo "</pre>";*/ $file = $_FILES; //2.判断是否是post上传 /*if(!is_uploaded_file($file['av']['tmp_name'])){ exit("文件非法上传!"); }*/ //3.判断文件上传是否发生了错误 if($file['av']['error']>0){ switch($file['av']['error']){ case 1: exit('超过php.ini当中的upload_max_filesize的值'); case 2: exit('超过html表单设定的MAX_FILE_SIZE的值'); case 3: exit('文件只有部分被上传'); case 4: exit('文件没有被上传'); case 6: exit('找不到临时文件夹'); case 7: exit('写入失败!'); default: exit('发生未知错误'); } } //4.判断文件mime类型 $mime = $file['av']['type']; $mimes = array('image/bmp','image/gif','image/jpeg','image/png','image/x-icon'); if(!in_array($mime,$mimes)){ exit('文件mime类型不被允许'); } //5.判断上传文件的扩展名 $name = $file['av']['name']; //获取扩展名 //1.字符串翻转,找最后一个.,分割成数组,找数组的第一个元素,再翻转过来 //2.直接找最后一个.的位置,通过substr截取 //3.strrchr //4.pathinfo('....',PATHINFO_EXTENSION) $ext = pathinfo($name,PATHINFO_EXTENSION); $exts = array('gif','png','jpeg','jpg','ico','bmp'); if(!in_array($ext,$exts)){ exit('文件扩展名不被允许'); } //6.判断文件大小 if($file['av']['size']>8){ exit('文件大小超出范围,请修改后上传'); } //7.建立文件夹,生成新的文件名 $dir = "./uploads/".date("Y/m/d");//2013/08/08 if(!file_exists($dir)){ mkdir($dir,0755,true);//./uploads/2013/08/08 } $new_name = md5(time().mt_rand()).".".$ext; //8.移动缓存文件到指定目录 if(!move_uploaded_file($file['av']['tmp_name'],$dir."/".$new_name)){ exit("很遗憾!文件上传失败!"); } //9.将以上有用数据保存到数组当中 $info = array('new_name'=>$new_name,'dir'=>$dir,'size'=>$file['av']['size'],'type'=>$mime,'ext'=>$ext); echo "亲!恭喜您!上传成功啦!";
文件上传类
<?php function upload(&$info,$up="./uploads",$name='av',$mimes=array('image/gif'),$exts=array('gif'),$size=3000000){ //1.观察数组 $file = $_FILES[$name]; //2.判断错误 if($file['error']>0){ switch($file['error']){ case 1: $info = '超过php.ini当中的upload_max_filesize的值'; return false; case 2: $info = '超过html表单设定的MAX_FILE_SIZE的值'; return false; case 3: $info = '文件只有部分被上传'; return false; case 4: $info = '文件没有被上传'; return false; case 6: $info = '找不到临时文件夹'; return false; case 7: $info = '写入失败!'; return false; default: $info = '发生未知错误'; return false; } } //3.判断是否是post上传 if(!is_uploaded_file($file['tmp_name'])){ $info = "文件非法上传"; return false; } //4.判断mime类型 if(!in_array($file['type'],$mimes)){ $info = "文件mime类型不被允许"; return false; } //5.判断后缀 $ext = pathinfo($file['name'],PATHINFO_EXTENSION); if(!in_array($ext,$exts)){ $info = "文件上传扩展名不被允许"; return false; } //6.判断大小 if($file['size']>$size){ $info = "上传大小超出指定范围"; return false; } //7.新建目录,新建文件名 $dir = rtrim($up,"/")."/".date("Y/m/d"); if(!file_exists($dir)){ mkdir($dir,0755,true); } $new_name = md5(time().mt_rand()).".".$ext; //8.移动文件(核心代码) if(move_uploaded_file($file['tmp_name'],$dir."/".$new_name)){ $info = "亲!恭喜您!上传文件成功!"; //9.返回上传文件的信息 return array('name'=>$file['name'],'new_name'=>$new_name,'dir'=>$dir,'size'=>$file['size'],'ext'=>$ext); } }
类的调用:
<?php
include "./2-upload.func.php";
$info = "";
$arr = upload($info,'./uploads',"av",array('image/jpeg'),array('jpg'));
echo $info;
var_dump($arr);
多文件上传:
<form action="multiple_upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="300000000000000" />
片名:<input type="text" name="name" /><p />
请上传大片:<input type="file" name="av[]" /><p />
请上传大片:<input type="file" name="av[]" /><p />
请上传大片:<input type="file" name="av[]" /><p />
请上传大片:<input type="file" name="av[]" /><p />
请上传大片:<input type="file" name="av[]" /><p />
请上传大片:<input type="file" name="av[]" /><p />
<!--请上传大片:<input type="file" name="av1" /><p />
请上传大片:<input type="file" name="av2" /><p />
请上传大片:<input type="file" name="av3" /><p />
请上传大片:<input type="file" name="av4" /><p />
请上传大片:<input type="file" name="av5" /><p />
请上传大片:<input type="file" name="av6" /><p />-->
<input type="submit" />
</form>
<?php
echo "<pre>";
var_dump($_FILES);
echo "</pre>";
//include "2-upload.func.php";
/*foreach($_FILES as $key=>$val){//遍历大数组$_FILES将里面的所有的input type=file的内容处理一遍,这个只能处理av1,av2,av3,av4...这种类型的表单
upload($info,"./mul_uploads",$key,array("image/jpeg","image/gif","image/png"),array('gif','jpg','png'));
echo $info."<br />";//循环调用函数
}*/
foreach($_FILES['av'] as $key=>$val){//可以处理av[]这种格式的表单数据,每次大循环将name,type,tmp_name,error,size分别遍历出来
foreach($val as $k=>$v){//将对应的name/type/tmp_name/error/size的所有的内容全部遍历出来存入新数组
$arr["av"][$k][$key] = $v;//$arr['av'][0]['name']
//组装对应键的数组,每次小循环将对应的键name/type...的每个上传文件的内容对应存下来
}
}
/*$key = "name";
$val = array();
$k = 0;
$v = 'dasdasdasdas.jpg';
$arr["av"][0]["name"]='dasdasdasdas.jpg';
$k = 1;
$v = "dasdasdasdasd.jpg";
$arr['av'][1]["name"] = "";*/
echo "<pre>";
var_dump($arr);
echo "</pre>";
/*
array(1) {
["av"]=>
array(5) {
[0]=>
array(6) {
['name']=>
string(44) "8b13632762d0f703bdd1a0f309fa513d2697c5b9.jpg"
['type']=>
string(36) "51ccc01fjw1e6j1od94b8j208206o74u.jpg"
['tmp_name']=>
string(26) "2457331_131727867162_2.jpg"
['error']=>
string(44) "d52a2834349b033b40abf06214ce36d3d539bd62.jpg"
['size']=>
string(16) "Img259247193.jpg"
}
[1]=>
array(6) {
['name']=>
string(44) "8b13632762d0f703bdd1a0f309fa513d2697c5b9.jpg"
['type']=>
string(36) "51ccc01fjw1e6j1od94b8j208206o74u.jpg"
['tmp_name']=>
string(26) "2457331_131727867162_2.jpg"
['error']=>
string(44) "d52a2834349b033b40abf06214ce36d3d539bd62.jpg"
['size']=>
string(16) "Img259247193.jpg"
}
[2]=>
array(6) {
['name']=>
string(44) "8b13632762d0f703bdd1a0f309fa513d2697c5b9.jpg"
['type']=>
string(36) "51ccc01fjw1e6j1od94b8j208206o74u.jpg"
['tmp_name']=>
string(26) "2457331_131727867162_2.jpg"
['error']=>
string(44) "d52a2834349b033b40abf06214ce36d3d539bd62.jpg"
['size']=>
string(16) "Img259247193.jpg"
}
[3]=>
array(6) {
['name']=>
string(44) "8b13632762d0f703bdd1a0f309fa513d2697c5b9.jpg"
['type']=>
string(36) "51ccc01fjw1e6j1od94b8j208206o74u.jpg"
['tmp_name']=>
string(26) "2457331_131727867162_2.jpg"
['error']=>
string(44) "d52a2834349b033b40abf06214ce36d3d539bd62.jpg"
['size']=>
string(16) "Img259247193.jpg"
}
[4]=>
array(6) {
['name']=>
string(44) "8b13632762d0f703bdd1a0f309fa513d2697c5b9.jpg"
['type']=>
string(36) "51ccc01fjw1e6j1od94b8j208206o74u.jpg"
['tmp_name']=>
string(26) "2457331_131727867162_2.jpg"
['error']=>
string(44) "d52a2834349b033b40abf06214ce36d3d539bd62.jpg"
['size']=>
string(16) "Img259247193.jpg"
}
}
}
*/
读取上传文件
<?php
//1.告诉浏览器文件的类型
//echo $mime = mime_content_type("./mm.jpg");
header("Content-type:image/jpeg");
//2.告诉浏览器文件的大小
header("Content-Length:".filesize("./mm.jpg"));
//3.告诉浏览器以附件的形式打开文件
//header("Content-Disposition:attachment");
header("Content-Disposition:attachment;filename=mm.jpg");
//4.打开文件
readfile("./mm.jpg");
function getfile($dir){
if(!is_dir($dir)){
return false;
}
$d = opendir($dir);
while(false!==($f=readdir($d))){
if($f=="."||$f==".."){
continue;
}
$file = rtrim($dir,"/")."/".$f;
if(is_file($file)){
echo $f."---".filetype($file)."<br />";
}
if(is_dir($file)){
getfile($file);
}
}
}
getfile("./uploads");