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

[经验分享] HTML5:多文件上传 Upload multiple files at once with HTML5, jQuery and PHP

[复制链接]

尚未签到

发表于 2017-4-12 11:29:48 | 显示全部楼层 |阅读模式
n today’s modern web applications, uploading files to the server is a very common feature, however it is often the case that your application must allow multiple file uploads to the server. Today we will build a form that will allow the user to upload multiple files to the server at once using HTML5, jQuery and PHP. Let us being …

 

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.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>File Upload in PHP</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="css/default.css" rel="stylesheet" type="text/css" />
<link href="css/font-awesome.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="la-anim-10"></div><!-- .corner-preloader -->
<div id="wrapper">
<div id="upload-widget">
<h1>
<i class="fa fa-cloud-upload"></i> File Uploader
</h1>
<form id="file-upload-form">
<div class="file-input-wrap">
Browse Files
<input type="file" name="file" id="file" multiple>
</div><!-- .file-input-wrap -->
</form><!-- #file-upload-form -->
</div><!-- #upload-widget -->
<div id="upload-result">
</div><!-- #upload-result -->
</div><!-- #wrapper -->
<div id="preloader">
<img src="img/preloader.gif" alt="files uploading..." />
</div>
<!-- SCRIPTS -->
<script type="text/javascript" src="js/libraries/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/custom.js"></script>
</body>
</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
// loop through the array of files
foreach($_FILES as $index => $file)
{
// for easy access
$fileName     = $file['name'];
// for easy access
$fileTempName = $file['tmp_name'];
// check if there is an error for particular entry in array
if(!empty($file['error'][$index]))
{
// some error occurred with the file in index $index
// yield an error here
return false;
}
// check whether file has temporary path and whether it indeed is an uploaded file
if(!empty($fileTempName) && is_uploaded_file($fileTempName))
{
// move the file from the temporary directory to somewhere of your choosing
move_uploaded_file($fileTempName, "uploads/" . $fileName);
// mark-up to be passed to jQuery's success function.
echo '<p>Click <strong><a href="uploads/'%20.%20$fileName%20.%20'" target="_blank">' . $fileName . '</a></strong> to download it.</p>';
}
}
?>
 

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:

$(function() {
// grab the file input and bind a change event onto it
$('#file').bind("change", function() {
// new html5 formdata object.
var formData = new FormData();
//for each entry, add to formdata to later access via $_FILES["file" + i]
for (var i = 0, len = document.getElementById('file').files.length; i < len; i++) {
formData.append("file" + i, document.getElementById('file').files);
}
//send formdata to server-side
$.ajax({
url: "file-upload.php", // our php file
type: 'post',
data: formData,
dataType: 'html', // we return html from our php file
async: true,
processData: false,  // tell jQuery not to process the data
contentType: false,   // tell jQuery not to set contentType
success : function(data) {
$('#upload-result').append('<div class="alert alert-success"><p>File(s) uploaded successfully!</p><br />');
$('#upload-result .alert').append(data);
},
error : function(request) {
console.log(request.responseText);
}
});
});
});
 
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
 
 
 
 
 
 

运维网声明 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-363883-1-1.html 上篇帖子: PHPer谨记:10个重要的PHP网络信息函数说明 下篇帖子: 提高你的PHP编程效率需要注意的一些小细节
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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