標籤:io ar os sp for 檔案 on art cti
//今天才學的 留作紀念吧
//upload.php
<?php
class Upload
{
private $filename;//檔案名稱
private $type;//檔案類型
private $fileerror;//檔案錯誤
private $allowsize;//允許的檔案大小
private $allowtype;//允許的類型
private $path;//路徑
private $newsname;//檔案新名稱
private $tmpname;//快取檔案名稱
private $filetypes;//尾碼名
private $seterror;//錯誤
function __construct($files,$path)
{
$this->filename=$files[‘name‘];
$types=explode(".",$this->filename);//根據.截取檔案的尾碼名稱(.jpg)
$this->filetypes=$types[count($types)-1];//尾碼名稱(jpg)
$this->fileerror=$files[‘error‘];
$this->allowtype=array("jpg","png","gif","bmp","jpeg");
$this->allowsize=2000000;
$this->path=$path;//路徑
$this->newsname=time().".".$this->filetypes;
$this->tmpname=$files[‘tmp_name‘];//快取檔案
$this->seterror=1;
if($this->fileerror!=0)
{
switch($this->fileerror)
{
case 1:
echo "檔案大小超出限制";exit;break;
case 2:
echo "上傳檔案過大";exit;break;
case 3:
echo "檔案只有部分上傳";exit;break;
case 4:
echo "檔案沒有被上傳";exit;break;
case 6:
echo "找不到臨時檔案夾";exit;break;
case 7:
echo "檔案寫入失敗";exit;break;
}
}
else
{
if(!file_exists($this->path))
{
mkdir($this->path,0777);
}
else
{
if(!is_uploaded_file($this->tmpname))
{
echo "非法操作";exit;
}
else
{
if($this->seterror!=1)
{
echo $this->seterror;exit;
}
else
{
move_uploaded_file($this->tmpname,$this->path.$this->newsname);
}
}
}
}
}
function get_path_name()
{
return $this->path.$this->newsname;
}
}
?>
//do_upload.php頁面
<meta charset="utf-8">
<form method="post" action="do_upload.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="200000"/>
<input type="file" name="files" value="瀏覽"/>
<input type="submit" name="sub" value="上傳"/>
</form>
<?php
if(isset($_POST[‘sub‘]))
{
include "upload.php";
$up=new Upload($_FILES[‘files‘],"up/");
$imgname=$up->get_path_name();
?>
<img src="<?php echo $imgname ?>"/>
<?php
}
?>
初出茅廬的圖片上傳的封裝-- -- --物件導向