2 ini_set('display_startup_errors',1);
3 ini_set('display_errors',1);
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 // =============={ Configuration Begin }==============
25 'title' => 'strace.club',
27 // Directory to store uploaded files
30 // Display list uploaded files
33 // Allow users to delete files that they have uploaded (will enable sessions)
34 'allow_deletion' => true,
36 // Allow users to mark files as hidden
37 'allow_private' => true,
40 'listfiles_size' => true,
43 'listfiles_date' => true,
45 // Display file dates format
46 'listfiles_date_format' => 'F d Y H:i:s',
48 // Randomize file names (number of 'false')
49 'random_name_len' => 8,
51 // Keep filetype information (if random name is activated)
52 'random_name_keep_type' => true,
54 // Random file name letters
55 'random_name_alphabet' => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
57 // Display debugging information
60 // Complete URL to your directory (including tracing slash)
61 'url' => 'http://strace.club/',
63 // Amount of seconds that each file should be stored for (0 for no limit)
66 // Files that will be ignored
67 'ignores' => array('.', '..', 'LICENSE', 'README.md'),
69 // =============={ Configuration End }==============
74 $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME);
76 // Adding current script name to ignore list
77 $data['ignores'] = $settings['ignores'];
78 $data['ignores'][] = $data['scriptname'];
81 $data['uploaddir'] = realpath($settings['uploaddir']);
83 // Maximum upload size, set by system
84 $data['max_upload_size'] = ini_get('upload_max_filesize');
86 // If file deletion or private files are allowed, starting a session.
87 // This is required for user authentification
88 if ($settings['allow_deletion'] || $settings['allow_private']) {
92 if (!isset($_SESSION['upload_user_id']))
93 $_SESSION['upload_user_id'] = rand(100000, 999999);
95 // List of filenames that were uploaded by this user
96 if (!isset($_SESSION['upload_user_files']))
97 $_SESSION['upload_user_files'] = array();
100 // If debug is enabled, logging all variables
101 if ($settings['debug']) {
103 // Enabling error reporting
104 error_reporting(E_ALL);
107 // Displaying debug information
108 echo '<h2>Settings:</h2>';
113 // Displaying debug information
114 echo '<h2>Data:</h2>';
120 // Displaying debug information
121 echo '<h2>SESSION:</h2>';
128 function FormatSize ($bytes) {
129 $units = array('B', 'KB', 'MB', 'GB', 'TB');
131 $bytes = max($bytes, 0);
132 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
133 $pow = min($pow, count($units) - 1);
135 $bytes /= pow(1024, $pow);
137 return ceil($bytes) . ' ' . $units[$pow];
140 // Rotating a two-dimensional array
141 function DiverseArray ($vector) {
143 foreach ($vector as $key1 => $value1)
144 foreach ($value1 as $key2 => $value2)
145 $result[$key2][$key1] = $value2;
149 // Handling file upload
150 function UploadFile ($file_data) {
155 $data['uploaded_file_name'] = basename($file_data['name']);
156 $data['target_file_name'] = $file_data['uploaded_file_name'];
158 // Generating random file name
159 if ($settings['random_name_len'] !== false) {
161 $data['target_file_name'] = '';
162 while (strlen($data['target_file_name']) < $settings['random_name_len'])
163 $data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)];
164 if ($settings['random_name_keep_type'])
165 $data['target_file_name'] .= '.' . pathinfo($data['uploaded_file_name'], PATHINFO_EXTENSION);
166 } while (file_exists($data['target_file_name']));
168 $data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name'];
170 // Do now allow to overwriting files
171 if (file_exists($data['upload_target_file'])) {
172 echo 'File name already exists' . "\n";
176 // Moving uploaded file OK
177 if (move_uploaded_file($file_data['tmp_name'], $data['upload_target_file'])) {
178 if ($settings['allow_deletion'] || $settings['allow_private'])
179 $_SESSION['upload_user_files'][] = $data['target_file_name'];
180 echo $settings['url'] . $data['target_file_name'] . "\n";
182 echo 'Error: unable to upload the file.';
187 // Files are being POSEed. Uploading them one by one.
188 if (isset($_FILES['file'])) {
189 header('Content-type: text/plain');
190 if (is_array($_FILES['file'])) {
191 $file_array = DiverseArray($_FILES['file']);
192 foreach ($file_array as $file_data)
193 UploadFile($file_data);
195 UploadFile($_FILES['file']);
199 // Other file functions (delete, private).
201 if ($settings['allow_deletion'])
202 if ($_POST['action'] === 'delete')
203 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
204 if (file_exists($_POST['target'])) {
205 unlink($_POST['target']);
206 echo 'File has been removed';
210 if ($settings['allow_private'])
211 if ($_POST['action'] === 'privatetoggle')
212 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
213 if (file_exists($_POST['target'])) {
214 if ($_POST['target'][0] === '.') {
215 rename($_POST['target'], substr($_POST['target'], 1));
216 echo 'File has been made visible';
218 rename($_POST['target'], '.' . $_POST['target']);
219 echo 'File has been hidden';
225 // List files in a given directory, excluding certain files
226 function ListFiles ($dir, $exclude) {
227 $file_array = array();
229 while (false !== ($filename = readdir($dh)))
230 if (is_file($filename) && !in_array($filename, $exclude))
231 $file_array[filemtime($filename)] = $filename;
233 $file_array = array_reverse($file_array, true);
237 $file_array = ListFiles($settings['uploaddir'], $data['ignores']);
239 foreach ($file_array as $value) {
246 <meta charset="utf-8">
247 <title><?=$settings['title']?></title>
248 <style media="screen">
253 font-family: sans-serif;
258 background: rgba(255, 255, 255, 0.05);
266 background: rgba(255, 255, 255, 0.075);
289 background: rgba(255, 255, 255, 0.1);
291 text-decoration: none;
296 body > ul > li > a:hover {
300 body > ul > li > a:active {
304 body > ul > li > a > span {
309 body > ul > li > form {
310 display: inline-block;
315 body > ul > li.owned {
319 body > ul > li > form > button {
321 display: inline-block;
325 background: rgba(255, 255, 255, 0.1);
329 body > ul > li > form > button:hover {
333 body > ul > li > form > button:active {
337 body > ul > li.uploading {
338 animation: upanim 2s linear 0s infinite alternate;
352 <h1><?=$settings['title']?></h1>
353 <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
354 Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
355 <input type="file" name="file[]" multiple required id="simpleupload-input"/>
357 <?php if ($settings['listfiles']) { ?>
358 <ul id="simpleupload-ul">
360 foreach ($file_array as $mtime => $filename) {
361 $file_info = array();
363 $file_private = $filename[0] === '.';
365 if ($settings['listfiles_size'])
366 $file_info[] = FormatSize(filesize($filename));
368 if ($settings['listfiles_size'])
369 $file_info[] = date($settings['listfiles_date_format'], $mtime);
371 if ($settings['allow_deletion'] || $settings['allow_private'])
372 if (in_array(substr($filename, 1), $_SESSION['upload_user_files']) || in_array($filename, $_SESSION['upload_user_files']))
375 $file_info = implode(', ', $file_info);
377 if (strlen($file_info) > 0)
378 $file_info = ' (' . $file_info . ')';
384 if (!$file_private || $file_owner) {
385 echo "<li class=\"' . $class . '\">";
387 echo "<a href=\"$filename\" target=\"_blank\">$filename<span>$file_info</span></a>";
390 if ($settings['allow_deletion'])
391 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>';
393 if ($settings['allow_private'])
395 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>';
397 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>';
406 <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>
407 <script charset="utf-8">
408 var target_form = document.getElementById('simpleupload-form'),
409 target_ul = document.getElementById('simpleupload-ul'),
410 target_input = document.getElementById('simpleupload-input');
412 target_form.addEventListener('dragover', function (event) {
413 event.preventDefault();
416 function AddFileLi (name, info) {
417 target_form.style.display = 'none';
419 var new_li = document.createElement('li');
420 new_li.className = 'uploading';
422 var new_a = document.createElement('a');
423 new_a.innerHTML = name;
424 new_li.appendChild(new_a);
426 var new_span = document.createElement('span');
427 new_span.innerHTML = info;
428 new_a.appendChild(new_span);
430 target_ul.insertBefore(new_li, target_ul.firstChild);
433 function HandleFiles (event) {
434 event.preventDefault();
437 files = event.dataTransfer.files,
440 var form = new FormData();
442 for (; i < len; i++) {
443 form.append('file[]', files[i]);
444 AddFileLi(files[i].name, files[i].size + ' bytes');
447 var xhr = new XMLHttpRequest();
448 xhr.onload = function() {
449 window.location.reload();
452 xhr.open('post', '<?php echo $data['scriptname']; ?>', true);
456 target_form.addEventListener('drop', HandleFiles, false);
458 document.getElementById('simpleupload-input').onchange = function () {
459 AddFileLi('Uploading...', '');
460 target_form.submit();