標籤:io ar sp 檔案 on cti bs ad as
<?php
class Upload
{
private $doc; //檔案
private $docsize; //檔案大小
private $docname; //檔案名稱字
private $doctype; //檔案類型
public $docnewname; //圖片新名字
private $allowtype; //可以上傳的檔案類型
private $seterror=1; //錯誤原因
private $doctmpname; //檔案臨時名
private $path; //檔案儲存體路徑
function __construct($file,$path)
{
$this->doc=$file;
$this->path=$path;
$this->docsize=$file[‘size‘];
$this->docname=$file[‘name‘];
$this->doctmpname=$file[‘tmp_name‘];
$array=explode(‘.‘,$this->docname);
$allowtype=Array("jpg","jpeg","gif","png","bmp");
$this->doctype=$array[count($array)-1];
$this->docnewname=time().".".$this->doctype;
$error=$file[‘error‘];
if($error!=0)
{
switch($error)
{
case 1:$this->seterror="上傳的檔案過大,最大能上傳2M";break;
case 2:$this->seterror="上傳的檔案過大,最大能上傳2M";break;
case 3:$this->seterror="檔案只有部分被上傳";break;
case 4:$this->seterror="檔案沒有被上傳";break;
case 6:$this->seterror="找不到臨時檔案夾";break;
case 7:$this->seterror="檔案寫入失敗";break;
}
}
else
{
if(!in_array($this->doctype,$allowtype))
{
$this->seterror="檔案類型不符";
}
else
{
if(!is_uploaded_file($this->doctmpname))
{
$this->seterror="非法操作";
}
else
{
if(!file_exists($this->path))
{
mkdir($this->path,07777);
}
else
{
if(!move_uploaded_file($this->doctmpname,$this->path.$this->docnewname))
{
$this->seterror="檔案上傳失敗";
}
}
}
}
}
}
function get_img_newname()
{
if($this->seterror!=1)
{
echo $this->seterror;exit;
}
else
{
return $this->path.$this->docnewname;
}
}
}
?>
關於上傳圖片的類(有點粗糙)