1 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2 <?php
3 /**
4 * php文件上传类
5 * @file uploadclass.php
6 * @link http://www.xinui.com
7 * @author kamon <1003935728@qq.com>
8 * @copyright 2012
9 * @package UploadFile
10 * @version 0.2beta
11 * @LastModify 2010-7-9 22:36:05
12 */
13 class UploadFile {
14 public $Path;
15 public $UploadDir;
16 public $SaveName;
17 public $FileType;
18 public $MaxSize;
19 public $Err;
20 public $Uploaded;
21 public function __construct(){
22 $this->Path = '../';
23 $this->UploadDir = 'images/';
24 $this->SaveName = '';
25 $this->FileType = array('jpg','gif','bmp','png');
26 $this->MaxSize = 2048;
27 $this->Err = array();
28 $this->Uploaded = false;
29 }
30
31 public function Upload($upfile, $oldfile='',$is_create_dir=false){
32 if(is_uploaded_file($upfile['tmp_name'])){
33 if(!empty($upfile)){
34 if(substr($this->UploadDir,-1,1)!='/' && !empty($this->UploadDir)){
35 $this->UploadDir .= '/';
36 }
37 if(trim($this->SaveName)==''){
38 $this->SaveName = date('YmdHis',time()).rand(100,999);
39 }
40 $fileExt = '.'.end(explode('.', $upfile['name']));
41 $this->SaveName .= $fileExt;
42 $savefile = $this->UploadDir.$this->SaveName;
43 $imgType = explode('/',$upfile['type']);
44 if($upfile['size'] == 0){
45 $this->Err[] = '上传失败,文件大小为0字节!';
46 }
47 if(!in_array(strtolower(substr($fileExt,1)),$this->FileType)){
48 $this->Err[] = '不允许上传该类型的文件!('.$fileExt.')';
49 }
50 if($upfile['error'] !=0){
51 $this->Err[] = '未知错误,文件上传失败!';
52 }
53 if($upfile['size']/1024 > $this->MaxSize){
54 $this->Err[] = '文件大小超出上限,只允许上传大小为 '. $this->MaxSize . 'k的文件!';
55 }
56 if(!file_exists($this->Path.$this->UploadDir)){
57 @mkdir($this->Path.$this->UploadDir,0777);//创建文件目录
58 }
59 if(!file_exists($this->Path.$this->UploadDir) && trim($this->Path.$this->UploadDir)!=''){
60 if($is_create_dir){
61 $dir_path = explode('/',$this->UploadDir);
62 $sub_path = $this->Path;
63 foreach($dir_path as $dir){
64 $sub_path .= $dir.'/';
65 // @mkdir($sub_path,0777);
66 }
67 }else{
68
69 $this->Err[] = '上传目录不存在且设置为不允许创建目录!';
70 }
71 }
72 if(!$this->Err){
73 @move_uploaded_file($upfile['tmp_name'],$this->Path.$savefile);
74 if(trim($oldfile)!='' && file_exists($this->Path.$oldfile)){
75 @unlink($this->Path.$oldfile);
76 }
77 $this->Uploaded = true;
78 return $savefile;
79 }else{
80 @unlink($upfile['tmp_name']);
81 }
82 }
83 }
84 return trim($oldfile);
85 }
86 }
87 /**
88 //调用
89 //if(isset($_FILES['file'])){
90 $file = new UploadFile();
91 $file->Path = './../';
92 $file->UploadDir = "product/image/".date('Ymd');
93 $filename = $file->Upload($_FILES['file']);
94 if($file->Uploaded){
95 echo $filename;
96 }else{
97 print_r($file->Err);
98 }
99 }
100 */
101 ?>
内嵌页面(内嵌页面支持重新上传自动删除图片)
View Code
1 <?php include("uploadclass.php");
2 $myip="./../";
3 $delid=$_GET['delid'];
4 if(isset($delid)){
5 unlink($myip.$delid);
6 }
7 if(isset($_FILES['file'])){
8 $file = new UploadFile();
9 $file->Path = './../';
10 $file->UploadDir = "product/image/".date('Ymd');
11 $filename = $file->Upload($_FILES['file']);
12 if($file->Uploaded){
13
14 ?>
15 <script> parent.document.getElementById("addr").value='<?php echo $filename;?>';</script>
16 <form method="post" action="?delid=<?php echo $filename;?>">
17 <?php echo $filename;?><input type="submit" name="submit" value="重新上传"/>
18 </form>
19 <?php
20
21 }else{
22 //print_r($file->Err);
23 echo "<script>alert('上传文件错误错误');history.go(-1)</script>";
24 }
25 }else{
26 ?>
27 <form enctype="multipart/form-data" method="post" action="" name="upload" onsubmit="return ch();">
28 <input type="file" name="file"/>
29 <input type="submit" name="submit" value="上传"/>
30 </form>
31 <?php
32 }
33 ?>
34 <script>
35 function ch()
36 {
37 if(upload.file.value==""){
38 alert("上传文件不能为空");
39 upload.file.focus();
40 return false;
41 }
42 return true;
43 }
44 </script>