-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupload_demo.php
126 lines (101 loc) · 3.58 KB
/
upload_demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">
<style>
progress {
margin:10px 0 10px 0;
}
.content {
margin-top:60px;
}
</style>
<title>PHP 5.4 File Upload Progress Demo</title>
</head>
<body>
<article class="container">
<div class="content">
<header class="page-header">
<h1>PHP 5.4 File Upload Progress Demo</h1>
</header>
<div class="row">
<div class="span16">
<h2>Upload</h2>
<p>Select one or two files to upload (Max total size 2MB)</p>
<form action="/upload.php" method="POST" enctype="multipart/form-data" id="upload">
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="upload" />
<div class="clearfix">
<label for="file1">File 1</label>
<div class="input">
<input type="file" name="file1" id="file1" />
</div>
</div>
<div class="clearfix">
<label for="file2">File 1</label>
<div class="input">
<input type="file" name="file2" id="file2" />
</div>
</div>
<div class="actions">
<input type="submit" class="btn primary" value="Upload"/>
</div>
</form>
<h2>Progress</h2>
<progress max="1" value="0" id="progress"></progress>
<p id="progress-txt"></p>
</div>
</div>
</div>
</article>
<!-- File containing Jquery and the Jquery form plugin-->
<script src="/js/jquery.js"></script>
<script>
//Holds the id from set interval
var interval_id = 0;
$(document).ready(function(){
//jquery form options
var options = {
success: stopProgress, //Once the upload completes stop polling the server
error: stopProgress
};
//Add the submit handler to the form
$('#upload').submit(function(e){
//check there is at least one file
if($('#file1').val() == '' && $('#file2').val() == '')
{
e.preventDefault();
return;
}
//Poll the server for progress
interval_id = setInterval(function() {
$.getJSON('/progress.php', function(data){
//if there is some progress then update the status
if(data)
{
$('#progress').val(data.bytes_processed / data.content_length);
$('#progress-txt').html('Uploading '+ Math.round((data.bytes_processed / data.content_length)*100) + '%');
}
//When there is no data the upload is complete
else
{
$('#progress').val('1');
$('#progress-txt').html('Complete');
stopProgress();
}
})
}, 200);
$('#upload').ajaxSubmit();
e.preventDefault();
});
});
function stopProgress()
{
clearInterval(interval_id);
}
</script>
</body>
</html>