PHP + plupload.js實(shí)現(xiàn)多圖上傳并顯示進(jìn)度條加刪除實(shí)例代碼
PHP + plupload.js JS插件實(shí)現(xiàn)多圖上傳并顯示進(jìn)度條加刪除實(shí)例,廢話不多說,直接上代碼
HTML代碼:
<!DOCTYPE html> <head> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> <title>多圖上傳</title> <script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript" src="plupload.full.min.js"></script> </head> <body> <style type="text/css"> *{ margin:0px; padding:0px; font-family:Microsoft Yahei; box-sizing:border-box; -webkit-box-sizing:border-box;} .demo{max-width:640px; margin:0 auto; min-width:320px;} .ul_pics{ float:left;} .ul_pics li{float:left; margin:0px; padding:0px; margin-left:5px; margin-top:5px; position:relative; list-style-type:none; border:1px solid #eee; width:80px; height:80px;} .ul_pics li img{width:80px;height:80px;} .ul_pics li i{ position:absolute; top:0px; right:-1px; background:red; cursor:pointer; font-style:normal; font-size:10px; width:14px; height:14px; text-align:center; line-height:12px; color:#fff;} .progress{position:relative; margin-top:30px; background:#eee;} .bar {background-color: green; display:block; width:0%; height:15px; } .percent{position:absolute; height:15px; top:-18px; text-align:center; display:inline-block; left:0px; width:80px; color:#666; line-height:15px; font-size:12px; } .demo #btn{ width:80px; height:80px; margin-left:5px; margin-top:5px; border:1px dotted #c2c2c2; background:url(up.png) no-repeat center; background-size:30px auto; text-align:center; line-height:120px; font-size:30px; color:#666; float:left;} </style> <div class="demo"> <a href="javascript:void(0)" rel="external nofollow" id="btn"></a> <ul id="ul_pics" class="ul_pics clearfix"> </ul> </div> <script type="text/javascript"> var uploader = new plupload.Uploader({ //創(chuàng)建實(shí)例的構(gòu)造方法 runtimes: 'html5,flash,silverlight,html4', //上傳插件初始化選用那種方式的優(yōu)先級(jí)順序 browse_button: 'btn', // 上傳按鈕 url: "upimgs.php?get=upimg", //遠(yuǎn)程上傳地址 flash_swf_url: 'plupload/Moxie.swf', //flash文件地址 silverlight_xap_url: 'plupload/Moxie.xap', //silverlight文件地址 filters: { max_file_size: '10mb', //最大上傳文件大?。ǜ袷?00b, 10kb, 10mb, 1gb) mime_types: [ //允許文件上傳類型 {title: "files", extensions: "jpg,png,gif"} ] }, multipart_params:{ }, //文件上傳附加參數(shù) file_data_name:"upimg", //文件上傳的名稱 multi_selection: false, //true:ctrl多文件上傳, false 單文件上傳 init: { FilesAdded: function(up, files) { //文件上傳前 if ($("#ul_pics").children("li").length > 5) { alert("您上傳的圖片太多了!"); uploader.destroy(); } else { var li = ''; plupload.each(files, function(file) { //遍歷文件 li += "<li id='" + file['id'] + "'><div class='progress'><span class='bar'></span><span class='percent'>上傳中 0%</span></div></li>"; }); $("#ul_pics").append(li); uploader.start(); } }, UploadProgress: function(up, file) { //上傳中,顯示進(jìn)度條 var percent = file.percent; $("#" + file.id).find('.bar').css({"width": percent + "%"}); $("#" + file.id).find(".percent").text("上傳中 "+percent + "%"); }, FileUploaded: function(up, file, info) { //文件上傳成功的時(shí)候觸發(fā) var data = eval("(" + info.response + ")"); $("#" + file.id).html("<img src='" + this.url + "'/><i onclick='delimg(this)'>x</i><input type='hidden' name='' value='"+ this.url +"'>"); }, Error: function(up, err) { //上傳出錯(cuò)的時(shí)候觸發(fā) alert("error"); } } }); uploader.init(); function delimg(o){ var src = $(o).prev().attr("src"); $.post("upimgs.php?get=delimg&imgurl="+src,function(data){ if(data==1){ $(o).parent().remove(); } }) } </script> </body> </html>
PHP 代碼:
<?php $typeArr = array("jpg", "png", "gif");//允許上傳文件格式 $path = "files/";//上傳路徑 if (isset($_POST)) { if($_GET['get']=="upimg"){ $name = $_FILES['file']['name']; $size = $_FILES['file']['size']; $name_tmp = $_FILES['file']['tmp_name']; if (empty($name)) { echo json_encode(array("error"=>"您還未選擇圖片")); exit; } $type = strtolower(substr(strrchr($name, '.'), 1)); //獲取文件類型 if (!in_array($type, $typeArr)) { echo json_encode(array("error"=>"清上傳jpg,png或gif類型的圖片!")); exit; } if ($size > (1024 * 1024 * 10)) { echo json_encode(array("error"=>"圖片大小已超過10MB!")); exit; } $pic_name = time() . rand(10000, 99999) . "." . $type;//圖片名稱 $pic_url = $path . $pic_name;//上傳后圖片路徑+名稱 if (move_uploaded_file($name_tmp, $pic_url)) { //臨時(shí)文件轉(zhuǎn)移到目標(biāo)文件夾 echo json_encode(array("error"=>"0","pic"=>$pic_url,"name"=>$pic_name)); } else { echo json_encode(array("error"=>"上傳有誤,清檢查服務(wù)器配置!")); } } if($_GET['get']=="delimg"){ $imgsrc = $_GET['imgurl']; unlink($imgsrc); echo 1; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于JS實(shí)現(xiàn)視頻上傳顯示進(jìn)度條
- JS+html5實(shí)現(xiàn)異步上傳圖片顯示上傳文件進(jìn)度條功能示例
- 原生JS上傳大文件顯示進(jìn)度條 php上傳文件代碼
- js實(shí)現(xiàn)帶進(jìn)度條提示的多視頻上傳功能
- 原生javascript上傳圖片帶進(jìn)度條【實(shí)例分享】
- 利用Plupload.js解決大文件上傳問題, 帶進(jìn)度條和背景遮罩層
- JS插件plupload.js實(shí)現(xiàn)多圖上傳并顯示進(jìn)度條
- js HTML5 Ajax實(shí)現(xiàn)文件上傳進(jìn)度條功能
- PHP中使用Session配合Javascript實(shí)現(xiàn)文件上傳進(jìn)度條功能
- JS實(shí)現(xiàn)上傳文件顯示進(jìn)度條
相關(guān)文章
PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能
這篇文章主要介紹了PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能,通過ajax實(shí)現(xiàn)主界面,php處理上傳文件,具體實(shí)例代碼大家跟隨腳本之家小編一起看看吧2018-06-06100多行PHP代碼實(shí)現(xiàn)socks5代理服務(wù)器[2]
這篇文章主要介紹了100多行PHP代碼實(shí)現(xiàn)socks5代理服務(wù)器,需要的朋友可以參考下2016-05-05PHP論壇實(shí)現(xiàn)積分系統(tǒng)的思路代碼詳解
這篇文章主要介紹了PHP論壇實(shí)現(xiàn)積分系統(tǒng)的思路,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),文中通過代碼給大家補(bǔ)充介紹了thinkphp刪除圖片的方法實(shí)現(xiàn)代碼,需要的朋友可以參考下2020-06-06使用php get_headers 判斷URL是否有效的解決辦法
本篇文章介紹了,使用php get_headers 判斷URL是否有效的解決辦法。需要的朋友參考下2013-04-04CodeIgniter配置之config.php用法實(shí)例分析
這篇文章主要介紹了CodeIgniter配置之config.php用法,結(jié)合實(shí)例形式詳細(xì)的分析了CodeIgniter中配置文件config.php的參數(shù)含義及具體使用技巧,需要的朋友可以參考下2016-01-01Laravel 5.1 框架Blade模板引擎用法實(shí)例分析
這篇文章主要介紹了Laravel 5.1 框架Blade模板引擎用法,結(jié)合實(shí)例形式分析了laravel5.1框架Blade模板引擎基本功能、創(chuàng)建、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-01-01