搜索 |
<?php
// 说明文件上传以后是按照/fujian/***/年月/年月日/文件名保存的
class UploadFile {
var $user_post_file = array(); //用户上传的文件
var $save_file_path; //存放用户上传文件的路径
var $max_file_size; //文件最大尺寸
var $last_error; //记录最后一次出错信息
var $allow_type = array('gif', 'jpg', 'png', 'zip', 'rar', 'txt', 'doc', 'pdf', 'txt');//默认允许用户上传的文件类型
var $final_file_path; //最终保存的文件名
var $save_info = array(); //返回一组有用信息,用于提示用户。
function UploadFile($file, $size ='', $type = '') {
$this->user_post_file = $file;
$this->max_file_size = $size; //如果用户不填写文件大小,则默认为2M.
if ($type != ''){
$this->allow_type = $type;
}
}
function upload($dirdes) {
$file_paths=" ";$file_names=" ";
$dirfirst="../fujian/".$dirdes;
!is_dir($dirfirst) ? mkdir($dirfirst) : null;
$dirsecond=$dirfirst."/".date("Ym",time());
!is_dir($dirsecond) ? mkdir($dirsecond) : null;
$dirthird=$dirsecond."/".date("YmdH",time()); //文件名
!is_dir($dirthird) ? mkdir($dirthird) : null;
$this->save_file_path = $dirthird;
for ($i = 0; $i < count($this->user_post_file['name']); $i++) {
//如果当前文件上传功能,则执行下一步。
if ($this->user_post_file['error'][$i] == 0) {
//取当前文件名、临时文件名、大小、扩展名,后面将用到。
$name = $this->user_post_file['name'][$i];
$tmpname = $this->user_post_file['tmp_name'][$i];
$size = $this->user_post_file['size'][$i];
$mime_type = $this->user_post_file['type'][$i];
$type = $this->getFileExt($this->user_post_file['name'][$i]);
//检测当前上传文件大小是否合法。
if (!$this->checkSize($size)) {
$this->last_error = "上传的附件超出了限制. 附件名: ".$name;
$this->halt($this->last_error);
continue;
}
//检测当前上传文件扩展名是否合法。
if (!$this->checkType($type)) {
$this->last_error = "不允许上传的类型: .".$type." 附件名: ".$name;
$this->halt($this->last_error);
continue;
}
//检测当前上传文件是否非法提交。
if(!is_uploaded_file($tmpname)) {
$this->last_error = "非法提交,附件名: ".$name;
$this->halt($this->last_error);
continue;
}
//移动文件后,重命名文件用。
$basename = $this->getBaseName($name, ".".$type);
// 转换存储编码
$fjname = iconv('utf-8','gb2312',$basename);
//移动后的文件名
$saveas = $fjname."_".time().".".$type;
$saveaswj = $basename."_".time().".".$type;
//组合新文件名再存到指定目录下,格式:存储路径 + 文件名 + 时间 + 扩展名
$this->final_file_path = $this->save_file_path."/".$saveas;
$this->final_file_path1 = $this->save_file_path."/".$saveaswj;
if(!move_uploaded_file($tmpname, $this->final_file_path)) {
$this->last_error = $this->user_post_file['error'][$i];
$this->halt($this->last_error);
continue;
}
$file_path='';$file_name="";
$file_path=explode("..",$this->final_file_path1);
$file_paths.=$file_path[1].";";
$file_names.=$name.";";
//存储当前文件的有关信息,以便其它程序调用。
$this->save_info[] = array(
"name" => $name,
"type" => $type,
"mime_type" => $mime_type,
"size" => $size,
"saveas" => $saveaswj,
"path" => $this->final_file_path1
);
}
}
$files_info=array(
'filesnames'=>$file_names,
'filespaths'=>$file_paths,
'num'=>count($this->save_info)
);
return $files_info;
}
function getSaveInfo() {
return $this->save_info;
}
function checkSize($size) {
if ($size > $this->max_file_size) {
return false;
}else {
return true;
}
}
function checkType($extension) {
foreach ($this->allow_type as $type) {
if (strcasecmp($extension , $type) == 0) return true;
}
return false;
}
function halt($msg) {
echo "<script>alert('您有未成功的附件\\n\\n 错误信息:\\n\\n" .$msg."'".")</script>";
// printf("<b>UploadFile Error:</b> %s <br>\n", $msg);
}
function getFileExt($filename) {
$stuff = pathinfo($filename);
return $stuff['extension'];
}
function getBaseName($filename, $type) {
$filename=preg_replace('/^.+[\\\\\\/]/', '', $filename);
$filename=explode($type,$filename);
return $filename[0];
}
}
// 实例使用
// require 'uploadfile.php';
// $type = array('gif', 'jpg', 'png', 'zip', 'rar', 'txt','doc','xls','docx','xlsx','wps','et','dps','ppt','pptx','pdf');
// $size="20480000";
// $upload = new UploadFile($_FILES['upload_file'], $size, $type);
// //上传用户文件,返回int值,为上传成功的文件个数。
// $info = $upload->upload("yjfujian");//获取文件路径和文件名方便保存数据库
// $save_info=$upload->getSaveInfo();//获取上传文件的详细信息;
<form action="/action/fujiantest.php" method="post" name="sendForm" enctype="multipart/form-data">
<div style="width: 500px;float: left;"><input type="file" name="upload_file[]" size=25> <span id="addfile"></span></div>
<label for=""><INPUT style="margin-top: 5px;" name="123" class="button_white_zengj" type=BUTTON onclick='var atobj = document.all("addfile");
atobj.outerHTML = "<br><input type=file size=25 name=upload_file[]>" + atobj.outerHTML;' VALUE="增加附件"></label>
<div><input type="submit" value="发送" /></div>
</form>