jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码


Posted in PHP onOctober 15, 2014

本例用到其他2个php class.upload.php和 functions.php还有css和js以及img文件

完整实例代码点击此处本站下载。

效果图如下:

jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码

实现代码如下:

JavaScript代码如下:

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

javascript代码如下:

<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代码如下:

<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  

/* 

    @@ 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>';  

}

希望本文所述对大家的PHP+jQuery程序设计有所帮助。

PHP 相关文章推荐
转换中文日期的PHP程序
Oct 09 PHP
PHP小程序自动提交到自助友情连接
Nov 24 PHP
PHP学习之整理字符串
Apr 17 PHP
php读取富文本的时p标签会出现红线是怎么回事
May 13 PHP
PHP 9 大缓存技术总结
Sep 17 PHP
thinkPHP引入类的方法详解
Dec 08 PHP
PHP解耦的三重境界(浅谈服务容器)
Mar 13 PHP
PHP封装的PDO数据库操作类实例
Jun 21 PHP
利用PHP扩展Xhprof分析项目性能实践教程
Sep 05 PHP
Laravel关联模型中过滤结果为空的结果集(has和with区别)
Oct 18 PHP
TP5框架使用QueryList采集框架爬小说操作示例
Mar 26 PHP
php引用传递
Apr 01 PHP
PHP实现文件下载断点续传详解
Oct 15 #PHP
PHP多进程编程实例
Oct 15 #PHP
PHP实现采集中国天气网未来7天天气
Oct 15 #PHP
跟我学Laravel之视图 &amp; Response
Oct 15 #PHP
跟我学Laravel之请求与输入
Oct 15 #PHP
跟我学Laravel之路由
Oct 15 #PHP
跟我学Laravel之请求(Request)的生命周期
Oct 15 #PHP
You might like
php下检测字符串是否是utf8编码的代码
2008/06/28 PHP
PHP 源代码压缩小工具
2009/12/22 PHP
php中强制下载文件的代码(解决了IE下中文文件名乱码问题)
2011/05/09 PHP
国产PHP开发框架myqee新手快速入门教程
2014/07/14 PHP
PHP常见的6个错误提示及解决方法
2016/07/07 PHP
一个不错的应用,用于提交获取文章内容,不推荐用
2007/03/03 Javascript
Javascript继承机制的设计思想分享
2011/08/28 Javascript
IE与Firefox在JavaScript上的7个不同句法分享
2011/10/30 Javascript
Javascript和HTML5利用canvas构建Web五子棋游戏实现算法
2013/07/17 Javascript
javascript里绝对用的上的字符分割函数总结
2014/07/31 Javascript
js全选按钮的实现方法
2015/11/17 Javascript
jQuery UI结合Ajax创建可定制的Web界面
2016/06/22 Javascript
Vue-router的使用和出现空白页,路由对象属性详解
2018/09/03 Javascript
vscode 开发Vue项目的方法步骤
2018/11/25 Javascript
JS数组扁平化、去重、排序操作实例详解
2020/02/24 Javascript
VUE使用axios调用后台API接口的方法
2020/08/03 Javascript
[04:56]经典回顾:前Ehome 与 前LGD
2015/02/26 DOTA
Python的Flask站点中集成xhEditor文本编辑器的教程
2016/06/13 Python
Python入门_条件控制(详解)
2017/05/16 Python
python中ASCII码字符与int之间的转换方法
2018/07/09 Python
python从入门到精通 windows安装python图文教程
2019/05/18 Python
Python中logging日志库实例详解
2020/02/19 Python
python Canny边缘检测算法的实现
2020/04/24 Python
使用pth文件添加Python环境变量方式
2020/05/26 Python
scrapy-redis分布式爬虫的搭建过程(理论篇)
2020/09/29 Python
python实现文件分片上传的接口自动化
2020/11/19 Python
一款纯css3实现的非常实用的鼠标悬停特效演示
2014/11/05 HTML / CSS
值得收藏的HTML5资源(学习html5的朋友可以收藏下)
2010/07/20 HTML / CSS
台湾家适得:Homeget
2019/02/11 全球购物
RealTek面试题
2016/06/28 面试题
static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?
2015/02/22 面试题
文秘档案管理岗位职责
2014/03/06 职场文书
孩子满月酒答谢词
2015/09/30 职场文书
公司年会主持词范文!
2019/05/07 职场文书
MySQL如何构建数据表索引
2021/05/13 MySQL
Python各协议下socket黏包问题原理
2022/04/12 Python