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+DBM的同学录程序(5)
Oct 09 PHP
Windows中安装Apache2和PHP4权威指南
Nov 18 PHP
php microtime获取浮点的时间戳
Feb 21 PHP
解析:php调用MsSQL存储过程使用内置RETVAL获取过程中的return值
Jul 03 PHP
PHP+Memcache实现wordpress访问总数统计(非插件)
Jul 04 PHP
php查找指定目录下指定大小文件的方法
Nov 28 PHP
浅析THINKPHP的addAll支持的最大数据量
Feb 03 PHP
php页面,mysql数据库转utf-8乱码,utf-8编码问题总结
Aug 27 PHP
Yii2中关联查询简单用法示例
Aug 10 PHP
php 从一个数组中随机的取出若干个不同的数实例
Dec 31 PHP
PHP设计模式之装饰器模式定义与用法详解
Apr 02 PHP
Laravel 前端资源配置教程
Oct 18 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&amp;mysql(二)
2006/10/09 PHP
基于mysql的论坛(6)
2006/10/09 PHP
php allow_url_include的应用和解释
2010/04/22 PHP
php格式化时间戳
2016/12/17 PHP
php 中的closure用法详解
2017/06/12 PHP
jquery 日期分离成年月日的代码
2010/05/14 Javascript
第一次接触神奇的Bootstrap表单
2016/07/27 Javascript
COM组件中调用JavaScript函数详解及实例
2017/02/23 Javascript
AngularJS表格样式简单设置方法示例
2017/03/03 Javascript
详解vue 模拟后台数据(加载本地json文件)调试
2017/08/25 Javascript
mui上拉加载更多下拉刷新数据的封装过程
2017/11/03 Javascript
bootstrap表格内容过长时用省略号表示的解决方法
2017/11/21 Javascript
使用gulp构建前端自动化的方法示例
2018/12/25 Javascript
vue生命周期的探索
2019/04/03 Javascript
基于Vue+elementUI实现动态表单的校验功能(根据条件动态切换校验格式)
2019/04/04 Javascript
更优雅的微信小程序骨架屏实现详解
2019/08/07 Javascript
node删除、复制文件或文件夹示例代码
2019/08/13 Javascript
JavaScript实现左右滚动电影画布
2020/02/06 Javascript
js实现百度登录窗口拖拽效果
2020/03/19 Javascript
Vue computed 计算属性代码实例
2020/04/22 Javascript
python实现得到一个给定类的虚函数
2014/09/28 Python
Python的Django框架中使用SQLAlchemy操作数据库的教程
2016/06/02 Python
Python单例模式的两种实现方法
2017/08/14 Python
Python3一行代码实现图片文字识别的示例
2018/01/15 Python
如何在VSCode上轻松舒适的配置Python的方法步骤
2019/10/28 Python
将不规则的Python多维数组拉平到一维的方法实现
2021/01/11 Python
python爬虫scrapy基于CrawlSpider类的全站数据爬取示例解析
2021/02/20 Python
清除canvas画布内容(点擦除+线擦除)
2020/08/12 HTML / CSS
台湾网购生鲜第一品牌:i3Fresh爱上新鲜
2017/10/26 全球购物
大学校园毕业自我鉴定
2014/01/15 职场文书
保密工作承诺书
2014/08/29 职场文书
2014坚持党风廉政建设思想汇报
2014/09/18 职场文书
国庆节演讲稿范文2014
2014/09/19 职场文书
三严三实对照检查材料思想汇报
2014/09/28 职场文书
离婚纠纷代理词
2015/05/23 职场文书
红灯733-1型14管5波段半导体收音机
2021/04/22 无线电