php + layui 文件上传 以及 拖拽上传
阅读原文时间:2023年07月09日阅读:1

HTML:
  

            <div id="box">  
                <div id="drop\_area">将文件拖拽到此区域</div>  
                <div id="area\_img"> <img src="../images/tzsc.png" id="img\_upfiles"  style="cursor: pointer"/> </div>  
            </div>  

<div class="layui-form-item">  
    <div class="layui-input-block">  
        <div class="layui-upload" style="margin-left: 730px">  
            <button type="button" class="layui-btn layui-btn-normal" id="upfiles">&emsp;<i class="layui-icon"></i> 文件上传&emsp;</button>  
            <span style="color: #666"></span>  
        </div>  
        <table id="tabl1" class="layui-table" style="width: 850px;margin-top: -38px;margin-left: -110px;">  
            <colgroup>  
                <col width="300"><col width="80"><col width="160"><col width="300">  
            </colgroup>  
            <thead>  
            <tr>  
                <th>文件名称</th><th>类型</th><th>操作</th><th>备注</th>  
            </tr>  
            </thead>  
            <tbody id="fileList"></tbody>  
        </table>  
    </div>  
</div>

JS:

XMLhttpReuest.js

function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
return xmlHttp;
}

{?*拖拽上传*?}

php:

ajax.files.upload.php

header("Content-Type:text/html;charset=UTF-8");$CODE = $_GET['upfilecode'];
$PATH = $_GET['path'];

$a = new UPFILE($CODE, $PATH, $_FILES['file'], $pl);

class UPFILE
{
public $pl = '';
public $file = '';

public function \_\_construct($code, $path, $file, $pl)  
{  
    if ($code == '')  
        $this->ExtFrm(1, "缺少上传编码");  
    if ($path == '')  
        $this->ExtFrm(2, "缺少上传路径");  
    if (!$file)  
        $this->ExtFrm(3, "找不到上传文件");  
    if ($file\['size'\] > 1024 \* 1024 \* 50) {  
        $this->ExtFrm(3, "上传失败,文件大小超过限制(文件大小:50MB)");  
    }  
    $size=round($file\['size'\]/1024/1024, 2);  
    $this->pl = $pl;  
    $this->file = $file;  
    $this->type = $path;

    $filePath = $this->createFile($path);  
    $fileOld = $this->verifyFile();  
    $this->fileupload($code, $filePath, $fileOld,$size);  
}

/\*\*  
 \* Method:createFile  
 \* Desc:创建文件路径  
 \*/  
public function createFile($path)  
{  
    $Ym = date('Ym');  
    $filepath = $Ym . ($path != "" ? "/" : "") . $path;  
    $fullpath = '../upfiles/' . $filepath;  
    if (!is\_dir($fullpath)) {  
        $res = mkdir($fullpath, 0777, true);  
    }  
    return array($filepath . "/", $fullpath . "/");  
}

/\*\*  
 \* Method:verifyFile  
 \* Desc:验证文件格式  
 \*/  
public function verifyFile()  
{  
    $pinfo = pathinfo($this->file\["name"\]);  
    $extension = strtolower($pinfo\['extension'\]);  
    switch ($this->type) {  
        case 'message\_img': //消息推送 - 富文本 图片上传  
            $file\_format = array("jpg", "jpeg", "gif", "png");  
            if (!in\_array($extension, $file\_format)) {  
                $this->ExtFrm(4, "上传失败,文件格式不正确(支持:jpg,gif,png)");  
            }  
            break;  
        default:  
            $file\_format = array("jpg", "jpeg", "gif", "png", "rar", "zip", "doc", "docx", "xls", "xlsx", "pdf", "txt", "ppt", "pptx", "tif",'mp4');  
            if (!in\_array($extension, $file\_format)) {  
                $this->ExtFrm(4, "上传失败,文件格式不正确(支持:jpg,gif,png,rar,zip,doc,docx,xls,xlsx,pdf,txt,ppt,pptx,tif,mp4)");  
            }  
            break;  
    }  
    return array("filename" => $pinfo\['filename'\], "extension" => $extension);  
}

/\*\*  
 \* Method:fileupload  
 \* Desc:上传文件  
 \*/  
public function fileupload($code, $filepath, $fileold,$size)  
{  
    $old\_name = $fileold\['filename'\];  
    $new\_name = time() . rand(0, 500000) . dechex(rand(0, 10000));  
    move\_uploaded\_file($this->file\['tmp\_name'\], $filepath\[1\] . $new\_name . "." . $fileold\['extension'\]);

    if ($this->type != 'message\_img') {  
        $sql = "insert into system\_attach (\`BM\`,\`YFJMC\`,\`XFJMC\`,\`WJLJ\`,\`WJLX\`,\`CJSJ\`,\`CJRID\`,\`CJRMC\`,\`SIZE\`) values (:BM,:YFJMC,:XFJMC,:WJLJ,:WJLX,:CJSJ,:CJRID,:CJRMC,:SIZE)";  
        $this->pl->query($qry, $sql, array(  
            ":BM" => $code,  
            ":YFJMC" => $old\_name . ".",  
            ":XFJMC" => $new\_name . "." . $fileold\['extension'\],  
            ":WJLJ" => $filepath\[0\],  
            ":WJLX" => $fileold\['extension'\],  
            ":CJSJ" => date("Y-m-d H:i:s"),  
            ":CJRID" => $\_SESSION\['UID'\],  
            ":CJRMC" => $\_SESSION\['NAME'\],  
            ":SIZE" => $size,  
        ));  
        $id = $this->pl->insert\_id();  
    }  
    $this->ExtFrm(0, "上传成功", $id, $filepath\[1\], $new\_name, $old\_name, $fileold\['extension'\]);  
}

/\*\*  
 \* Method:extFrm  
 \* Desc:结果返回  
 \*/  
public function extFrm($code, $msg, $id = 0, $filepath = "", $new\_name = '', $old\_name = '', $file\_ext = "")  
{  
    if ($code > 0) {  
        $ExtFrm = array("code" => $code, "msg" => $msg);  
    } else if ($this->type == 'message\_img') {  
        $title = $new\_name . "." . $file\_ext;  
        $ExtFrm = array("code" => $code, "msg" => $msg, "data" => array("src" => $filepath . $title, "title" => $title));  
    } else {  
        $ExtFrm = array("code" => $code, "msg" => $msg, "id" => $id, "path" => $filepath, "new\_name" => $new\_name, "old\_name" => $old\_name, "file\_ext" => $file\_ext);  
    }  
    exit(json\_encode($ExtFrm));  
}  

}

?>