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/'
56 // Amount of seconds that each file should be stored for (0 for no limit)
60 // =============={ Configuration End }==============
69 $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME);
72 $data['uploaddir'] = realpath($settings['uploaddir']);
74 // Maximum upload size, set by system
75 $data['max_upload_size'] = ini_get('upload_max_filesize');
77 if ($settings['allow_deletion'] || $settings['allow_private']) {
80 if (!isset($_SESSION['upload_user_id']))
81 $_SESSION['upload_user_id'] = rand(1000, 9999);
83 if (!isset($_SESSION['upload_user_files']))
84 $_SESSION['upload_user_files'] = array();
87 if ($settings['debug']) {
90 // Enabling error reporting
91 error_reporting(E_ALL);
94 // Displaying debug information
95 echo '<h2>Debugging information: settings</h2>';
100 // Displaying debug information
101 echo '<h2>Debugging information: data</h2>';
107 // Displaying debug information
108 echo '<h2>Debugging information: _SESSION</h2>';
114 function FormatSize ($bytes) {
115 $units = array('B', 'KB', 'MB', 'GB', 'TB');
117 $bytes = max($bytes, 0);
118 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
119 $pow = min($pow, count($units) - 1);
121 $bytes /= pow(1024, $pow);
123 return ceil($bytes) . ' ' . $units[$pow];
126 function DiverseArray ($vector) {
128 foreach($vector as $key1 => $value1)
129 foreach($value1 as $key2 => $value2)
130 $result[$key2][$key1] = $value2;
134 function UploadFile ($file_data) {
139 $data['uploaded_file_name'] = basename($file_data['name']);
140 $data['target_file_name'] = $file_data['uploaded_file_name'];
142 // Generating random file name
143 if ($settings['random_name_len'] !== false) {
145 $data['target_file_name'] = '';
146 while (strlen($data['target_file_name']) < $settings['random_name_len'])
147 $data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)];
148 if ($settings['random_name_keep_type'])
149 $data['target_file_name'] .= '.' . pathinfo($data['uploaded_file_name'], PATHINFO_EXTENSION);
150 } while (file_exists($data['target_file_name']));
152 $data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name'];
154 // Do now allow to rewrite files
155 if (file_exists($data['upload_target_file'])) {
156 echo 'File name already exists' . "\n";
160 // Moving uploaded file OK
161 if (move_uploaded_file($file_data['tmp_name'], $data['upload_target_file'])) {
162 if ($settings['allow_deletion'] || $settings['allow_private'])
163 $_SESSION['upload_user_files'][] = $data['target_file_name'];
164 echo $settings['url'] . $data['target_file_name'] . "\n";
166 echo 'Error: unable to upload the file.';
172 if (isset($_FILES['file'])) {
173 if ($settings['debug']) {
174 // Displaying debug information
175 echo '<h2>Debugging information: data</h2>';
179 // Displaying debug information
180 echo '<h2>Debugging information: file</h2>';
186 header('Content-type: text/plain');
187 if (is_array($_FILES['file'])) {
188 $file_array = DiverseArray($_FILES['file']);
189 foreach ($file_array as $file_data)
190 UploadFile($file_data);
192 UploadFile($_FILES['file']);
197 if ($settings['allow_deletion'])
198 if ($_POST['action'] === 'delete')
199 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
200 if (file_exists($_POST['target'])) {
201 unlink($_POST['target']);
202 echo 'File has been removed';
206 if ($settings['allow_private'])
207 if ($_POST['action'] === 'privatetoggle')
208 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
209 if (file_exists($_POST['target'])) {
210 if ($_POST['target'][0] === '.') {
211 rename($_POST['target'], substr($_POST['target'], 1));
212 echo 'File has been made visible';
214 rename($_POST['target'], '.' . $_POST['target']);
215 echo 'File has been hidden';
221 function ListFiles ($dir, $exclude) {
222 $file_array = array();
224 while (false !== ($filename = readdir($dh)))
225 if (is_file($filename) && !in_array($filename, $exclude))
226 $file_array[filemtime($filename)] = $filename;
228 $file_array = array_reverse($file_array, true);
235 <meta charset="utf-8">
236 <title>strace.club</title>
237 <style media="screen">
242 font-family: sans-serif;
247 background: rgba(255, 255, 255, 0.05);
255 background: rgba(255, 255, 255, 0.075);
278 background: rgba(255, 255, 255, 0.1);
280 text-decoration: none;
285 body > ul > li > a:hover {
289 body > ul > li > a:active {
293 body > ul > li > a > span {
298 body > ul > li > form {
299 display: inline-block;
304 body > ul > li.owned {
308 body > ul > li > form > button {
310 display: inline-block;
314 background: rgba(255, 255, 255, 0.1);
318 body > ul > li > form > button:hover {
322 body > ul > li > form > button:active {
326 body > ul > li.uploading {
327 animation: upanim 2s linear 0s infinite alternate;
342 <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
343 Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
344 <input type="file" name="file[]" multiple required id="simpleupload-input"/>
346 <?php if ($settings['listfiles']) { ?>
347 <ul id="simpleupload-ul">
349 $file_array = ListFiles($settings['uploaddir'], array('.', '..', $data['scriptname']));
350 foreach ($file_array as $mtime => $filename) {
351 $file_info = array();
353 $file_private = $filename[0] === '.';
355 if ($settings['listfiles_size'])
356 $file_info[] = FormatSize(filesize($filename));
358 if ($settings['listfiles_size'])
359 $file_info[] = date($settings['listfiles_date_format'], $mtime);
361 if ($settings['allow_deletion'] || $settings['allow_private'])
362 if (in_array(substr($filename, 1), $_SESSION['upload_user_files']) || in_array($filename, $_SESSION['upload_user_files']))
365 $file_info = implode(', ', $file_info);
367 if (strlen($file_info) > 0)
368 $file_info = ' (' . $file_info . ')';
374 if (!$file_private || $file_owner) {
375 echo "<li class=\"' . $class . '\">";
377 echo "<a href=\"$filename\" target=\"_blank\">$filename<span>$file_info</span></a>";
380 if ($settings['allow_deletion'])
381 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>';
383 if ($settings['allow_private'])
385 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>';
387 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>';
396 <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>
397 <script charset="utf-8">
398 var target_form = document.getElementById('simpleupload-form'),
399 target_ul = document.getElementById('simpleupload-ul'),
400 target_input = document.getElementById('simpleupload-input');
402 target_form.addEventListener('dragover', function (event) {
403 event.preventDefault();
406 function AddFileLi (name, info) {
407 target_form.style.display = 'none';
409 var new_li = document.createElement('li');
410 new_li.className = 'uploading';
412 var new_a = document.createElement('a');
413 new_a.innerHTML = name;
414 new_li.appendChild(new_a);
416 var new_span = document.createElement('span');
417 new_span.innerHTML = info;
418 new_a.appendChild(new_span);
420 target_ul.insertBefore(new_li, target_ul.firstChild);
423 function HandleFiles (event) {
424 event.preventDefault();
427 files = event.dataTransfer.files,
430 var form = new FormData();
432 for (; i < len; i++) {
433 form.append('file[]', files[i]);
434 AddFileLi(files[i].name, files[i].size + ' bytes');
437 var xhr = new XMLHttpRequest();
438 xhr.onload = function() {
439 window.location.reload();
442 xhr.open('post', '<?php echo $data['scriptname']; ?>', true);
446 target_form.addEventListener('drop', HandleFiles, false);
448 document.getElementById('simpleupload-input').onchange = function () {
449 AddFileLi('Uploading...', '');
450 target_form.submit();