-
Notifications
You must be signed in to change notification settings - Fork 4
/
ajaxUpload.php
141 lines (125 loc) · 5.16 KB
/
ajaxUpload.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
function getListFiles(){
$files = [];
if ($handle = opendir('uploads')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files[] = [
'name' => $file,
'time' => filemtime('uploads/' . $file)
];
}
}
closedir($handle);
usort($files, function($a, $b) {
return $b['time'] - $a['time'];
});
$list = "";
foreach ($files as $file) {
//$list .= '<li><a href="uploads/' . $file['name'] . '" download>' . $file['name'] . '</a> <a href="?' . USER . '=' . PASS . '&delete=' . rawurlencode($file['name']) . '" style="text-decoration:none;color:red;">✘</a></li>'; // GET
$list .= '<li><a href="uploads/' . $file['name'] . '" download>' . $file['name'] . '</a> <a href="#" data-file="' . htmlspecialchars($file['name']) . '" class="delete-link" style="text-decoration:none;color:red;">✘</a></li>'; // POST
}
return $list;
}
}
$listFiles = "";
// update list files after upload new file
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_GET['updateList'])) {
echo getListFiles();
return;
} elseif (isset($_POST['delete'])) {
$fileToDelete = $_POST['delete'];
$filePath = 'uploads/' . $fileToDelete;
$listDeleted = [];
if (file_exists($filePath)) {
unlink($filePath);
}
} else {
$list = getListFiles();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
function startUpload(){
document.getElementById('f1_upload_process').style.visibility = 'visible';
document.getElementById('f1_upload_form').style.visibility = 'hidden';
return true;
}
function stopUpload(success){
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
refreshList();
}
else {
result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
}
document.getElementById('f1_upload_process').style.visibility = 'hidden';
document.getElementById('f1_upload_form').innerHTML = result + '<label>File: <input name="myfile" type="file" size="30" /><\/label><br><br><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
document.getElementById('f1_upload_form').style.visibility = 'visible';
return true;
}
function refreshList() {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'ajaxUpload.php?updateList=1', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
document.getElementById('file-list').innerHTML = xhr.responseText;
attachDeleteHandlers(); // Attach delete handlers after refreshing the list
}
};
xhr.send();
}
function attachDeleteHandlers() {
var deleteLinks = document.getElementsByClassName('delete-link');
for (var i = 0; i < deleteLinks.length; i++) {
deleteLinks[i].addEventListener('click', function(event) {
event.preventDefault();
var fileName = this.getAttribute('data-file');
deleteFile(fileName);
});
}
}
function deleteFile(fileName) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'ajaxUpload.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
refreshList(); // Refresh the list after deleting the file
}
};
xhr.send('delete=' + encodeURIComponent(fileName));
}
document.addEventListener('DOMContentLoaded', function() {
attachDeleteHandlers();
});
</script>
</head>
<body>
<div id="container">
<div id="header"><div id="header_left"></div>
<div id="header_main">AJAX File Uploader</div><div id="header_right"></div></div>
<div id="content">
<form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
<p id="f1_upload_process" >Loading...<br/><img src="loader.gif" /><br/></p>
<p id="f1_upload_form" align="center"><br/>
<label>File:
<input name="myfile" type="file" size="30" /><br><br>
</label>
<label>
<input type="submit" name="submitBtn" class="sbtn" value="Upload" />
</label>
</p>
<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
</div>
<h2>List of files:</h2>
<ul id="file-list"><?php echo $list ?? ""; ?></ul>
</div>
</body>