//今天才学的 留作纪念吧
//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
}
?>