HTML5:多文件上传 Upload multiple files at once with HTML5, jQuery and PHP

The HTML

Nothing too fancy here, but do pay attention to the multipart attribute on the input element of type of file. This essentially tells the application to expect multiple files instead of a maximum of one.

Html代码   收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8" />  
  5.         <title>File Upload in PHP</title>  
  6.         <!--[if IE]>  
  7.             <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>  
  8.         <![endif]-->  
  9.         <link href="css/default.css" rel="stylesheet" type="text/css" />  
  10.         <link href="css/font-awesome.min.css" rel="stylesheet" type="text/css" />  
  11.     </head>  
  12.     <body>  
  13.         <div class="la-anim-10"></div><!-- .corner-preloader -->  
  14.         <div id="wrapper">  
  15.             <div id="upload-widget">  
  16.                 <h1>  
  17.                     <i class="fa fa-cloud-upload"></i> File Uploader  
  18.                 </h1>  
  19.   
  20.                 <form id="file-upload-form">  
  21.                     <div class="file-input-wrap">  
  22.                         Browse Files  
  23.                         <input type="file" name="file" id="file" multiple>  
  24.                     </div><!-- .file-input-wrap -->  
  25.                 </form><!-- #file-upload-form -->  
  26.             </div><!-- #upload-widget -->  
  27.   
  28.             <div id="upload-result">  
  29.             </div><!-- #upload-result -->  
  30.         </div><!-- #wrapper -->  
  31.   
  32.         <div id="preloader">  
  33.             <img src="img/preloader.gif" alt="files uploading..." />  
  34.         </div>  
  35.           
  36.         <!-- SCRIPTS -->  
  37.         <script type="text/javascript" src="js/libraries/jquery-2.1.1.min.js"></script>  
  38.         <script type="text/javascript" src="js/custom.js"></script>  
  39.     </body>  
  40. </html>  

 

Note also the scripts at the bottom of the html markup. You will need the jQuery library and some jQuery code which will come later. This will sit in the custom.js file.

 

PHP Code

Next comes the PHP code. In your favorite code editor program (I used Sublime Text 3 for this) create a new file and call it file-upload.php. In there we simply loop through the $_FILES array given to us by PHP and for each entry we do what needs to be done!

Php代码   收藏代码
  1. <?php  
  2.     // loop through the array of files  
  3.     foreach($_FILES as $index => $file)  
  4.     {  
  5.         // for easy access  
  6.         $fileName     = $file['name'];  
  7.         // for easy access  
  8.         $fileTempName = $file['tmp_name'];  
  9.         // check if there is an error for particular entry in array  
  10.         if(!emptyempty($file['error'][$index]))  
  11.         {  
  12.             // some error occurred with the file in index $index  
  13.             // yield an error here  
  14.             return false;  
  15.         }  
  16.   
  17.         // check whether file has temporary path and whether it indeed is an uploaded file  
  18.         if(!emptyempty($fileTempName) && is_uploaded_file($fileTempName))  
  19.         {  
  20.             // move the file from the temporary directory to somewhere of your choosing  
  21.             move_uploaded_file($fileTempName"uploads/" . $fileName);  
  22.             // mark-up to be passed to jQuery's success function.  
  23.             echo '<p>Click <strong><a href="uploads/'%20.%20$fileName%20.%20'" target="_blank">' . $fileName . '</a></strong> to download it.</p>';  
  24.         }  
  25.     }  
  26. ?>  

 

jQuery custom code

We will handle the submitting of the form via jQuery, we will also handle what exactly gets sent. Let us being by creating a file named custom.js and inside we will place the following code:

Js代码   收藏代码
  1. $(function() {  
  2.     // grab the file input and bind a change event onto it  
  3.     $('#file').bind("change"function() {  
  4.         // new html5 formdata object.  
  5.         var formData = new FormData();  
  6.         //for each entry, add to formdata to later access via $_FILES["file" + i]  
  7.         for (var i = 0, len = document.getElementById('file').files.length; i < len; i++) {  
  8.             formData.append("file" + i, document.getElementById('file').files[i]);  
  9.         }  
  10.   
  11.         //send formdata to server-side  
  12.         $.ajax({  
  13.             url: "file-upload.php"// our php file  
  14.             type: 'post',  
  15.             data: formData,  
  16.             dataType: 'html'// we return html from our php file  
  17.             async: true,  
  18.             processData: false,  // tell jQuery not to process the data  
  19.             contentType: false,   // tell jQuery not to set contentType  
  20.             success : function(data) {  
  21.                 $('#upload-result').append('<div class="alert alert-success"><p>File(s) uploaded successfully!</p><br />');  
  22.                 $('#upload-result .alert').append(data);  
  23.             },  
  24.             error : function(request) {  
  25.                 console.log(request.responseText);  
  26.             }  
  27.         });  
  28.     });  
  29. });  

 

Note that we make use of HTML5’s FormData, so this will not work on older browsers. Always check caniuse.com for browser compatibilities.

 

In the HTML, ensure the referenced paths to the ‘custom.js’ file is correct. In my case, I have my custom.js file sitting inside a folder called js. I also have my ‘file-upload.php’ sitting next to my html file.

 

That is it! We are done. You now have a file input that can handle the upload of multiple files are once.

 

Note: If you are developing on your local machine and you’re using WAMP or XAMP, there are certain restrictions set in the php.ini file. These basically limit the size of the files to be uploaded and also how much time is allowed for a file to be uploaded. If you plan on uploading big files, ensure you update those settings to meet your need.

 

 

原文:http://www.code-hound.com/upload-multiple-files-at-once-with-jquery-and-php/

转自:HTML5:多文件上传 Upload multiple files at once with HTML5, jQuery and PHP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值