设为首页 收藏本站
查看: 316|回复: 0

[经验分享] jquery php ajax多图片上传.上传进度,生成缩略图

[复制链接]

尚未签到

发表于 2015-8-26 10:49:41 | 显示全部楼层 |阅读模式
  本例用到其他2个php class.upload.php和 functions.php还有css和js以及img文件
  下载地址为www.freejs.net/demo/91/down.zip

DSC0000.jpg
演示  


JavaScript Code

  • <script type="text/javascript">  
  • $(document).ready(function() {  
  •     $("#filelist").niceScroll({  
  •         cursorwidth: "8px",  
  •         cursorborderradius: "0px",  
  •         cursoropacitymin: 0.1,  
  •         cursoropacitymax: 0.3  
  •     });  
  •   
  •     $(".side-pane").niceScroll({  
  •         cursorwidth: "8px",  
  •         cursorborderradius: "0px",  
  •         cursoropacitymin: 0.1,  
  •         cursoropacitymax: 0.3  
  •     });  
  •   
  •     $(".time").timeago();  
  • });  
  • </script>  
  


XML/HTML Code

  • <script type="text/javascript">  
  •         // <![CDATA[

  •             $('#upload_button').click(function() {
  •                 $('.side-pane').html('');
  •                 $('#upload_button').hide();
  •                 $('#pickfiles').hide();
  •                 $('#upload_info').show();
  •                 $('#upload_info').css("display","inherit");
  •                 uploader.start();

  •                 $('#filelist').block({
  •                     message: '<center><div class="start-message">Upload in Progress</div></center>',
  •                     css: {
  •                         border: 'none',
  •                         backgroundColor: 'none'
  •                     },
  •                     overlayCSS: {
  •                         backgroundColor: '#fff',
  •                         opacity: '0',
  •                         cursor: 'wait'
  •                     }
  •                 });
  •             });

  •             var uploader = new plupload.Uploader({
  •                 runtimes : 'flash, html5',
  •                 browse_button : 'pickfiles',
  •                 container : 'uploader',
  •                 max_file_size : '10mb',
  •                 url : 'upload.php',
  •                 flash_swf_url : 'uploader/uploader.swf',
  •                 filters : [
  •                     { title : "Image files", extensions : "jpg,jpeg,gif,png" }
  •                 ]
  •             });

  •             uploader.bind('Init', function(up, params) {});
  •             uploader.init();

  •             uploader.bind('FilesAdded', function(up, files) {
  •                 /*
  •                     @@ Show / hide various elements
  •                 */
  •                 $('.upload-message').hide();
  •                 $('.info-message').hide();
  •                 $('#upload_button').show();
  •                 $('#filelist_header').show();

  •                 $.each(files, function(i, file) {
  •                     $('#filelist').append(
  •                         '<div id="' + file.id + '" class="filecontainer">' +
  •                         '<div class="filename"> '+file.name + '</div>'+
  •                         '<div class="filesize">' + plupload.formatSize(file.size) + '</div>'+
  •                         '<div class="filestatus" id="status_'+file.id+'">0%</div>'+
  •                         '<div class="filedel"><a id="remove_'+file.id+'" href="javascript:;"><img src="img/uploader/delete.gif" /></a></div>' +
  •                         '</div>');

  •                     $('#remove_'+file.id).click(function(e) {
  •                         uploader.removeFile(file)
  •                         $('#'+file.id).hide('slow', function() { ('#'+file.id).remove(); });
  •                     });
  •                 });

  •                 up.refresh();
  •                 $('#filelist').animate({scrollTop: $('#filelist').attr("scrollHeight")}, 1500);
  •             });

  •             uploader.bind('UploadProgress', function(up, file) {
  •                 $('#status_' + file.id).html(file.percent + "%");
  •                     if(file.percent == 100) {
  •                         $('#' + file.id).block({
  •                             message: '',
  •                             css: {
  •                                 border: 'none',
  •                                 backgroundColor: 'none'
  •                             },
  •                             overlayCSS: {
  •                                 backgroundColor: '#fff',
  •                                 opacity: '0.7',
  •                                 cursor: 'wait'
  •                             }
  •                         });
  •                     }
  •                 $('#percent').width(uploader.total.percent+"%");
  •                 $('#upRate').text(Math.ceil(uploader.total.bytesPerSec/1024)+" kb/sec");
  •             });

  •             uploader.bind('FileUploaded', function(up, file, response) {
  •                 var input = $("#uploaded_photos");
  •                 var data = response.response;
  •                 var details = data.split(',');
  •                     if(details[0] == 'success') {
  •                         var photo_html = '<div class="padding-10 hm-photo clearfix"><a href="../upload/source/'+details[4]+'" target="_blank"><img src="../upload/small/'+details[4]+'"></a><p class="small-text light-text">'+details[1]+'</p><abbr class="time small-text" title="'+details[2]+'"></abbr></div>';
  •                         input.val(input.val() + details[1] + ":");
  •                     } else {
  •                         var photo_html = '<div class="clearfix">'+details[1]+'</div>';
  •                     }
  •                 $('.side-pane').prepend(photo_html);
  •                 $('.time').timeago();
  •             });

  •             uploader.bind('UploadComplete', function(up, files) {
  •                 $('#upload_info').hide();
  •                 $('#filelist').unblock({
  •                     onUnblock: function () {
  •                         $('#filelist_header').hide();
  •                         $('#filelist').html('<div style="margin-top: 180px; text-align: center;"><strong>ok</strong><br/>All photos have been uploaded.</div>');
  •                     }
  •                 });
  •             });

  •         // ]]>  
  •         </script>  
  上面2个js都放在index.php里面
  


XML/HTML Code

  • <div id="uploader" class="main-pane">  
  •             <div id="filelist_header" class="clearfix">  
  •                 <div class="filename">Name</div>  
  •                 <div class="filesize">Size</div>  
  •                 <div class="filestatus">Status</div>  
  •                 <div class="filedel"></div>  
  •                 <div></div>  
  •             </div>  
  •   
  •             <div id="filelist"></div>  
  •   
  •             <div class="action-btns">  
  •                 <a id="pickfiles" class="login-button btn">Select files to upload</a>  
  •                 <a href="javascript:;" id="upload_button" class="login-button upload hide">Upload</a>  
  •             </div>  
  •   
  •             <center>  
  •                 <div id="upload_info">  
  •                     <div id="upload_info_inner">  
  •                         <div class="progressbg">  
  •                             <div id="percent" class="progress"></div>  
  •                         </div>  
  •                     </div>  
  •   
  •                     <div id="unknown">  
  •                         <div id="time2go"></div>  
  •                         <div id="upRate"></div>  
  •                     </div>  
  •                 </div>  
  •             </center>  
  •   
  •             <form method="POST" action="process.php" id="processupload">  
  •                 <input type="hidden" name="uploaded_photos" id="uploaded_photos" />  
  •                 <input type="hidden" name="fkey" value="<?php echo $fkey; ?>" />  
  •             </form>  
  •         </div>  
  upload.php


PHP Code

  • <?php  
  • /*
  •     @@ Including the functions.php for using the necessary functions.
  • */  
  • include('functions.php');  
  •   
  • /*
  •     @@ This is the file upload class which does all the uploading work.
  • */  
  • include('class.upload.php');  
  •   
  • if(isset($_FILES["file"])) {  
  •     /*
  •         @@ Generating unique name for the photo.
  •     */  
  •     $time = time();  
  •     $rand_1 = rand(999, 999999);  
  •     $rand_2 = rand(999999, 999999999);  
  •     $rand_3 = rand();  
  •     $unique_value = $time.'_'.$rand_1.'_'.$rand_2.'_'.$rand_3;  
  •   
  •     /*
  •         @@ Folder creation for each and every day. This ensures performance even with millions of images.
  •     */  
  •     $folder = date('zY');  
  •         if(substr($folder, 0) == 0) {  
  •             $folder = '367'.date('Y');  
  •         }  
  •   
  •     /*
  •         @@ This folder is for the source image files.
  •     */  
  •     $pfolder = '../upload/source/';   
  •         if(!is_dir($pfolder)) {  
  •             mkdir($pfolder, 0755);  
  •         }  
  •   
  •     /*
  •         @@ This folder is for the image files with 120x120 dimensions.
  •     */  
  •     $tfolder = '../upload/small/';  
  •         if(!is_dir($tfolder)) {  
  •             mkdir($tfolder, 0755);  
  •         }  
  •   
  •     /*
  •         @@ Assigning the upload file to the upload class for all the processing.
  •     */  
  •     $handle = new Upload($_FILES["file"]);  
  •         if($handle->uploaded) {  
  •             $extension = $handle->file_src_name_ext;  
  •             $mime = $handle->file_src_mime;  
  •   
  •                 if(($mime == 'image/gif') || ($mime == 'image/jpg') || ($mime == 'image/png') || ($mime == 'image/bmp') || ($mime == 'image/pjpeg') | ($mime == 'image/jpeg')) {  
  •                     /*
  •                         @@ Check if the uploaded filetype is an image or not.
  •                     */  
  •                     if(($extension == 'gif') || ($extension == 'jpg') || ($extension == 'jpeg') || ($extension == 'png') || ($extension == 'bmp') || ($extension == 'pjpeg')) {  
  •                         if($handle->image_src_x > 500) {  
  •                                 /*
  •                                     @@ Check if the filesize is smaller than 10 MB. 10 MB = 10485760 bytes.
  •                                 */  
  •                             if($handle->file_src_size < 10485760) {  
  •                                 /*
  •                                     @@ Getting the actual file name (with and without extension) and sanitizing it for inserting in the database.
  •                                 */  
  •                                 $real_name = clean_input($handle->file_src_name);  
  •                                 $body_name = clean_input($handle->file_src_name_body);  
  •   
  •                                 $handle->file_new_name_body = $unique_value.'_'.$body_name;  
  •                                 $handle->Process($pfolder);  
  •   
  •                                 $handle->image_resize = true;  
  •                                 $handle->image_ratio_crop = 'T';  
  •                                 $handle->image_y = 120;  
  •                                 $handle->image_x = 120;  
  •                                 $handle->file_new_name_body = $unique_value.'_'.$body_name;  
  •                                 $handle->Process($tfolder);  
  •   
  •                                     if($handle->processed) {  
  •                                         $actual_name = $handle->file_dst_name;  
  •                                         $size = ceil($handle->file_src_size / 1024);  
  •   
  •                                         ## Sending photo details back to the uploader.  
  •                                         $date = date("c", $time);  
  •   
  •                                             ## Reducing the length of real name if it exceeds 50 characters.  
  •                                             if(strlen($real_name) > 50) {  
  •                                                 $real_name = substr($real_name, 0, 50).'..';  
  •                                             }  
  •                                             echo 'success,'.$real_name.','.$date.','.$folder.','.$actual_name;  
  •                                     } else {  
  •                                         echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>There was an error uploading the photo.</div>';  
  •                                     }  
  •                             } else {  
  •                                 echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>The photo is bigger than the allowed upload size of <strong>10MB</strong>.</div>';  
  •                             }  
  •                         } else {  
  •                             echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>You are trying to upload a photo with smaller dimensions.</div>';  
  •                         }  
  •                     } else {  
  •                         echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>Only photo uploads are allowed.</div>';  
  •                     }  
  •                 } else {  
  •                     echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>Only photo uploads are allowed.</div>';  
  •                 }  
  •         } else {  
  •             echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>An upload error occured.</div>';            
  •         }  
  •     /*
  •         @@ Return the json response to the script.
  •     */  
  •     $handle->Clean();  
  • } else {  
  •     echo 'error,<div class="alert alert-error"><strong>Upload Error</strong><br/>An upload error occured.</div>';  
  • }  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-104394-1-1.html 上篇帖子: php用soap创建webservice 下篇帖子: nginx中查看关于php的配置和php-fpm的重启等操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表