<?php
session_start();
$upload_file=$_FILES['file'];
$username=$_POST['username'];
//print_r($upload_file);
$cls_uploadFile=new cls_upload_file($username);
$cls_uploadFile->upload_file($upload_file);
class cls_upload_file{
private $upload_dir;
private $accept_fileType;
private $max_fileSize;
private $root_Dir;
const default_root_path = "../upload/tmp/";
//设置提示消息
static function feed_Msg($msg_code){
$msgs=array(
'F000'=>'Success',
'F100'=>'File Exist',
'F200'=>'Patch Create Failed',
'F300'=>'File Upload Wrong'
);
return $msgs[$msg_code];
}
//初始化构造函数,如果没有值则给默认路径
public function __construct($folder = "tmp1") {
//defalut file in tmp folder
if (trim($folder) == ""){
$folder = self::default_root_path."tmp1";
}else{
$folder=self::default_root_path.$folder;
}
$this->upload_dir = $folder;
}
//上传文件入口
public function upload_file($file){
$f_Name = $file['name'];
$f_tmpName = $file['tmp_name'];
$f_Size = $file['size'];
$f_Error = $file['error'];
$f_Type = $this->get_fileType($f_Name);
if($f_Error!=0){
echo self::feed_Msg('F300');
return false;
}
$dir = $this->upload_dir; //取得上传文件夹路径
//检测是否存在路径返回路径,不存在创建 返回路径
$target_dir = $this->checkDir_exist($dir);
if($target_dir<0){
echo self::feed_Msg('F200');
return false;
}
//拼接目标文件
$target_file=$target_dir."/".$f_Name;
//检查文件是否存在,不存在则返回文件名,反之返回-1
$target_file=$this->check_file_exist($target_file);
//文件存在 退出返回
if ($target_file<0){
echo self::feed_Msg('F100');
return false;
}
//移动临时文件到指定文件
move_uploaded_file($f_tmpName, $target_file);
echo self::feed_Msg('F000');
//$_SESSION['fname']="prefix-".md5($file['name']);
//echo "<br />";
//echo "FileName :".$_SESSION['fname'];
// echo json_encode("uploaded");
// header("Location: ../upload.php?name=fasdf");
}
//检查文件是否存在
private function check_file_exist($file){
if(!file_exists($file)){
return $file;
}else{
return -1;
}
}
private function check_file_size($file){
}
private function move_file($file,$new_location){
}
//获取文件类型
private function get_fileType($fileName){
$ftype= pathinfo($fileName, PATHINFO_EXTENSION);
return $ftype;
}
//检测文件夹是否存在,不存在创建,存在返回路径,创建失败返回-1
private function checkDir_exist($dir){
if(is_dir($dir)){
return $dir;
}else{
if(!mkdir($dir)){
return -1;
}
return $dir;
}
}
private function addFile_to_db(){
}
public function set_max_FileSize($size = -1){
$this->max_fileSize = $size;
}
public function set_accept_FileType($type = -1){
$this->accept_fileType = $type;
}
public function set_root_FolderDir($rootDir = -1){
$this->root_Dir = $rootDir;
}
}
?>
共同学习,写下你的评论
评论加载中...
作者其他优质文章