php 多檔案上傳經典代碼_PHP教程

來源:互聯網
上載者:User
require_once("include/upload.class.php");
if($_POST["button"])
{
//print_r($_FILES);
//多個上傳
//$upload = new TTRUpload($_FILES,"ANY");//同下

$upload = new TTRUpload(array($_FILES["file1"],$_FILES["file2"],$_FILES["file3"],$_FILES["file4"]),"ANY");

//單個上傳
//$upload = new TTRUpload($_FILES["file1"]);
$upload->upload();
echo $upload->getUploadFileName();
}
?>




Untitled Document




class TTRUpload extends Error
{
const filesize=81200000;
private $uploadpath="uploadfile/";
private $savepath=null;
private $uploadfilename=null; //單個檔案為檔案名稱,批量檔案為xxxx|xxxx格式,請注意
private $ext=array("jpg","gif","png");
private $error=null;
private $file=null;
private $uploadtype=null;
private $filename=null;

//建構函式,$type:ONE單個上傳 ANY批量上傳;
public function __construct($file,$type="ONE")
{
if($type!="ONE" && $type!="ANY")
{
echo "";
exit;
}
$this->uploadtype=$type;
$this->file=$file;
}

private function createFileName()
{
return $this->filename="TTR_".time().$this->getRandomN(4);
}

private function getUploadPath()
{
if(substr($this->uploadpath,-1,1)!="/")
{
$this->savepath=$this->uploadpath."/".date("Ym");
}else{
$this->savepath=$this->uploadpath.date("Ym");
}
$this->savepath=$this->getFolder($this->savepath);
return true;
}

private function getFileExt($tempfilename)
{
return end(explode(".",$tempfilename));
}

private function getExt()
{
if(in_array(strtolower($this->getFileExt($tempfilename)),$this->ext))
{
return true;
}else{
return false;
}
}

private function getFolder($folder)
{
if(!is_dir($folder))
{
mkdir($folder);
}
return $folder."/";
}


public function upload()
{
if($this->uploadtype=="ONE")
{

if($this->getExt($this->file["type"]))
{

parent::errorExt();

}else if($this->file["size"]>self::filesize){

parent::errorFileSize();

}else if(!$this->getUploadPath()){

parent::errorUploadPath();

}else{
$filenametemp=$this->createFileName();
$filename=$this->savepath.$filenametemp.".".$this->getFileExt($this->file["name"]);
if(move_uploaded_file($this->file["tmp_name"],$filename))
{
$this->uploadfilename=$filenametemp;
parent::okMoved();


}else{
parent::errorMoveUpload();
}
}
}else if($this->uploadtype=="ANY"){

for($i=0;$ifile);$i++)
{

if($this->getExt($this->file[$i]["type"]))
{
parent::errorExt();

}else if($this->file[$i]["size"]>self::filesize){

parent::errorFileSize();

}else if(!$this->getUploadPath()){

parent::errorUploadPath();

}else{
$filenametemp=$this->createFileName();
$filename=$this->savepath.$filenametemp.".".$this->getFileExt($this->file[$i]["name"]);
if(move_uploaded_file($this->file[$i]["tmp_name"],$filename))
{
$str.=$filenametemp."|";

}else{
parent::errorMoveUpload();
}

}

}
$this->uploadfilename=substr($str,0,strlen($str)-1);
parent::okMoved();
}
}

public function getUploadFileName()
{
return $this->uploadfilename;
}

public function setUploadPath($path)
{
$this->uploadpath=$path;
}


private function getRandomN($n)
{
if ($n < 1 || $n>10) return "";

$ary_num= array(0,1,2,3,4,5,6,7,8,9);
$return ="";
for ($i=0;$i<$n;$i++)
{
$randn = rand(0,9-$i);
$return .= $ary_num[$randn];
$ary_num[$randn] = $ary_num[9-$i];
}
return $return;
}



public function __destruct()
{
$this->uploadfilename=null;
$this->uploadtype=null;
$this->file=null;
$this->savepath=null;
}

}

class Error
{
public static function errorFileSize()
{
echo "超出最大上傳限制";
}

public static function errorExt()
{
echo "此類檔案不允許上傳";
}

public static function errorUploadPath()
{
echo "上傳路徑不正確";
}

public static function errorMoveUpload()
{
echo "上傳失敗";
}

public static function okMoved()
{
echo "上傳成功!";
}

public static function okArrayMoved()
{
echo "上傳成功!";
}
}

http://www.bkjia.com/PHPjc/632005.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632005.htmlTechArticle?php require_once(include/upload.class.php); if($_POST[button]) { //print_r($_FILES); //多個上傳 //$upload = new TTRUpload($_FILES,ANY);//同下 $upload = new TTRUpload(array($...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.