热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php+layui文件上传以及拖拽上传

HTML:将文件拖拽到此区域

HTML:
  



将文件拖拽到此区域





































文件名称类型操作备注




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));

}

}
?>

php + layui 文件上传 以及 拖拽上传的相关教程结束。



推荐阅读
author-avatar
大飞5678
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有