3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // =============={ Configuration Begin }==============
20 // Directory to store uploaded files
23 // Display list uploaded files
26 // Allow users to delete files that they have uploaded (will enable sessions)
27 allow_deletion => true,
29 // Allow users to mark files as hidden
30 allow_private => true,
33 listfiles_size => true,
36 listfiles_date => true,
38 // Display file dates format
39 listfiles_date_format => 'F d Y H:i:s',
41 // Randomize file names (number of 'false')
44 // Keep filetype information (if random name is activated)
45 random_name_keep_type => true,
47 // Random file name letters
48 random_name_alphabet => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
50 // Display debugging information
53 // Complete URL to your directory (including tracing slash)
54 url => 'http://strace.club/'
57 // =============={ Configuration End }==============
66 $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME);
69 $data['uploaddir'] = realpath($settings['uploaddir']);
71 // Maximum upload size, set by system
72 $data['max_upload_size'] = ini_get('upload_max_filesize');
74 if ($settings['allow_deletion'] || $settings['allow_private']) {
77 if (!isset($_SESSION['upload_user_id']))
78 $_SESSION['upload_user_id'] = rand(1000, 9999);
80 if (!isset($_SESSION['upload_user_files']))
81 $_SESSION['upload_user_files'] = array();
84 if ($settings['debug']) {
87 // Enabling error reporting
88 error_reporting(E_ALL);
91 // Displaying debug information
92 echo '<h2>Debugging information: settings</h2>';
97 // Displaying debug information
98 echo '<h2>Debugging information: data</h2>';
104 // Displaying debug information
105 echo '<h2>Debugging information: _SESSION</h2>';
111 function FormatSize ($bytes) {
112 $units = array('B', 'KB', 'MB', 'GB', 'TB');
114 $bytes = max($bytes, 0);
115 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
116 $pow = min($pow, count($units) - 1);
118 $bytes /= pow(1024, $pow);
120 return ceil($bytes) . ' ' . $units[$pow];
123 function DiverseArray ($vector) {
125 foreach($vector as $key1 => $value1)
126 foreach($value1 as $key2 => $value2)
127 $result[$key2][$key1] = $value2;
131 function UploadFile ($file_data) {
136 $data['uploaded_file_name'] = basename($file_data['name']);
137 $data['target_file_name'] = $file_data['uploaded_file_name'];
139 // Generating random file name
140 if ($settings['random_name_len'] !== false) {
142 $data['target_file_name'] = '';
143 while (strlen($data['target_file_name']) < $settings['random_name_len'])
144 $data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)];
145 if ($settings['random_name_keep_type'])
146 $data['target_file_name'] .= '.' . pathinfo($data['uploaded_file_name'], PATHINFO_EXTENSION);
147 } while (file_exists($data['target_file_name']));
149 $data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name'];
151 // Do now allow to rewrite files
152 if (file_exists($data['upload_target_file'])) {
153 echo 'File name already exists' . "\n";
157 // Moving uploaded file OK
158 if (move_uploaded_file($file_data['tmp_name'], $data['upload_target_file'])) {
159 if ($settings['allow_deletion'] || $settings['allow_private'])
160 $_SESSION['upload_user_files'][] = $data['target_file_name'];
161 echo $settings['url'] . $data['target_file_name'] . "\n";
163 echo 'Error: unable to upload the file.';
169 if (isset($_FILES['file'])) {
170 if ($settings['debug']) {
171 // Displaying debug information
172 echo '<h2>Debugging information: data</h2>';
176 // Displaying debug information
177 echo '<h2>Debugging information: file</h2>';
183 header('Content-type: text/plain');
184 if (is_array($_FILES['file'])) {
185 $file_array = DiverseArray($_FILES['file']);
186 foreach ($file_array as $file_data)
187 UploadFile($file_data);
189 UploadFile($_FILES['file']);
194 if ($settings['allow_deletion'])
195 if ($_POST['action'] === 'delete')
196 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
197 if (file_exists($_POST['target'])) {
198 unlink($_POST['target']);
199 echo 'File has been removed';
203 if ($settings['allow_private'])
204 if ($_POST['action'] === 'privatetoggle')
205 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
206 if (file_exists($_POST['target'])) {
207 if ($_POST['target'][0] === '.') {
208 rename($_POST['target'], substr($_POST['target'], 1));
209 echo 'File has been made visible';
211 rename($_POST['target'], '.' . $_POST['target']);
212 echo 'File has been hidden';
218 function ListFiles ($dir, $exclude) {
219 $file_array = array();
221 while (false !== ($filename = readdir($dh)))
222 if (is_file($filename) && !in_array($filename, $exclude))
223 $file_array[filemtime($filename)] = $filename;
225 $file_array = array_reverse($file_array, true);
232 <meta charset="utf-8">
233 <title>strace.club</title>
234 <style media="screen">
239 font-family: sans-serif;
244 background: rgba(255, 255, 255, 0.05);
252 background: rgba(255, 255, 255, 0.075);
275 background: rgba(255, 255, 255, 0.1);
277 text-decoration: none;
282 body > ul > li > a:hover {
286 body > ul > li > a:active {
290 body > ul > li > a > span {
295 body > ul > li > form {
296 display: inline-block;
301 body > ul > li.owned {
305 body > ul > li > form > button {
307 display: inline-block;
311 background: rgba(255, 255, 255, 0.1);
315 body > ul > li > form > button:hover {
319 body > ul > li > form > button:active {
323 body > ul > li.uploading {
324 animation: upanim 2s linear 0s infinite alternate;
339 <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
340 Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
341 <input type="file" name="file[]" multiple required id="simpleupload-input"/>
343 <?php if ($settings['listfiles']) { ?>
344 <ul id="simpleupload-ul">
346 $file_array = ListFiles($settings['uploaddir'], array('.', '..', $data['scriptname']));
347 foreach ($file_array as $mtime => $filename) {
348 $file_info = array();
350 $file_private = $filename[0] === '.';
352 if ($settings['listfiles_size'])
353 $file_info[] = FormatSize(filesize($filename));
355 if ($settings['listfiles_size'])
356 $file_info[] = date($settings['listfiles_date_format'], $mtime);
358 if ($settings['allow_deletion'] || $settings['allow_private'])
359 if (in_array(substr($filename, 1), $_SESSION['upload_user_files']) || in_array($filename, $_SESSION['upload_user_files']))
362 $file_info = implode(', ', $file_info);
364 if (strlen($file_info) > 0)
365 $file_info = ' (' . $file_info . ')';
371 if (!$file_private || $file_owner) {
372 echo "<li class=\"' . $class . '\">";
374 echo "<a href=\"$filename\" target=\"_blank\">$filename<span>$file_info</span></a>";
377 if ($settings['allow_deletion'])
378 echo '<form action="' . $data['scriptname'] . '" method="POST"><input type="hidden" name="target" value="' . $filename . '" /><input type="hidden" name="action" value="delete" /><button type="submit">delete</button></form>';
380 if ($settings['allow_private'])
382 echo '<form action="' . $data['scriptname'] . '" method="POST"><input type="hidden" name="target" value="' . $filename . '" /><input type="hidden" name="action" value="privatetoggle" /><button type="submit">make public</button></form>';
384 echo '<form action="' . $data['scriptname'] . '" method="POST"><input type="hidden" name="target" value="' . $filename . '" /><input type="hidden" name="action" value="privatetoggle" /><button type="submit">make private</button></form>';
393 <a href="https://github.com/muchweb/simple-php-upload"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>
394 <script charset="utf-8">
395 var target_form = document.getElementById('simpleupload-form'),
396 target_ul = document.getElementById('simpleupload-ul'),
397 target_input = document.getElementById('simpleupload-input');
399 target_form.addEventListener('dragover', function (event) {
400 event.preventDefault();
403 function AddFileLi (name, info) {
404 target_form.style.display = 'none';
406 var new_li = document.createElement('li');
407 new_li.className = 'uploading';
409 var new_a = document.createElement('a');
410 new_a.innerHTML = name;
411 new_li.appendChild(new_a);
413 var new_span = document.createElement('span');
414 new_span.innerHTML = info;
415 new_a.appendChild(new_span);
417 target_ul.insertBefore(new_li, target_ul.firstChild);
420 function HandleFiles (event) {
421 event.preventDefault();
424 files = event.dataTransfer.files,
427 var form = new FormData();
429 for (; i < len; i++) {
430 form.append('file[]', files[i]);
431 AddFileLi(files[i].name, files[i].size + ' bytes');
434 var xhr = new XMLHttpRequest();
435 xhr.onload = function() {
436 window.location.reload();
439 xhr.open('post', '<?php echo $data['scriptname']; ?>', true);
443 target_form.addEventListener('drop', HandleFiles, false);
445 document.getElementById('simpleupload-input').onchange = function () {
446 AddFileLi('Uploading...', '');
447 target_form.submit();