原文鏈接:http://www.cnblogs.com/itcx/p/4164333.html
<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.觀察數(shù)組
/*echo "<pre>";
var_dump($_FILES);
echo "</pre>";*/
$file = $_FILES;
//2.判斷是否是post上傳
/*if(!is_uploaded_file($file['av']['tmp_name'])){
exit("文件非法上傳!");
}*/
//3.判斷文件上傳是否發(fā)生了錯誤
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('發(fā)生未知錯誤');
}
}
//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.字符串翻轉(zhuǎn),找最后一個.,,分割成數(shù)組,,找數(shù)組的第一個元素,再翻轉(zhuǎn)過來
//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.將以上有用數(shù)據(jù)保存到數(shù)組當中
$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.觀察數(shù)組
$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 = '發(fā)生未知錯誤';
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);
}
}
類的調(diào)用:
<?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){//遍歷大數(shù)組$_FILES將里面的所有的input type=file的內(nèi)容處理一遍,,這個只能處理av1,av2,av3,av4...這種類型的表單
upload($info,"./mul_uploads",$key,array("image/jpeg","image/gif","image/png"),array('gif','jpg','png'));
echo $info."<br />";//循環(huán)調(diào)用函數(shù)
}*/
foreach($_FILES['av'] as $key=>$val){//可以處理av[]這種格式的表單數(shù)據(jù),,每次大循環(huán)將name,type,tmp_name,error,size分別遍歷出來
foreach($val as $k=>$v){//將對應的name/type/tmp_name/error/size的所有的內(nèi)容全部遍歷出來存入新數(shù)組
$arr["av"][$k][$key] = $v;//$arr['av'][0]['name']
//組裝對應鍵的數(shù)組,每次小循環(huán)將對應的鍵name/type...的每個上傳文件的內(nèi)容對應存下來
}
}
/*$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");
?
轉(zhuǎn)載于:https://www.cnblogs.com/itcx/p/4164333.html
來源:https://www./content-1-327601.html
|