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 ==============
21 // Directory to store uploaded files
24 // Display list uploaded files
27 // Allow users to delete files that they have uploaded (will enable sessions)
28 allow_deletion => true,
30 // Allow users to mark files as hidden
31 allow_private => true,
34 listfiles_size => true,
37 listfiles_date => true,
39 // Display file dates format
40 listfiles_date_format => 'F d Y H:i:s',
42 // Randomize file names (number of 'false')
45 // Keep filetype information (if random name is activated)
46 random_name_keep_type => true,
48 // Random file name letters
49 random_name_alphabet => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
51 // Display debugging information
54 // Complete URL to your directory (including tracing slash)
55 url => 'http://strace.club/',
59 // ============== Configuration end ==============
64 $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME);
67 $data['uploaddir'] = realpath($settings['uploaddir']);
69 // Maximum upload size, set by system
70 $data['max_upload_size'] = ini_get('upload_max_filesize');
72 if ($settings['allow_deletion'] || $settings['allow_private']) {
75 if (!isset($_SESSION['upload_user_id']))
76 $_SESSION['upload_user_id'] = rand(1000, 9999);
78 if (!isset($_SESSION['upload_user_files']))
79 $_SESSION['upload_user_files'] = array();
82 if ($settings['debug']) {
85 // Enabling error reporting
86 error_reporting(E_ALL);
89 // Displaying debug information
90 echo '<h2>Debugging information: settings</h2>';
95 // Displaying debug information
96 echo '<h2>Debugging information: data</h2>';
102 // Displaying debug information
103 echo '<h2>Debugging information: _SESSION</h2>';
109 function FormatSize ($bytes) {
110 $units = array('B', 'KB', 'MB', 'GB', 'TB');
112 $bytes = max($bytes, 0);
113 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
114 $pow = min($pow, count($units) - 1);
116 $bytes /= pow(1024, $pow);
118 return ceil($bytes) . ' ' . $units[$pow];
121 function DiverseArray ($vector) {
123 foreach($vector as $key1 => $value1)
124 foreach($value1 as $key2 => $value2)
125 $result[$key2][$key1] = $value2;
129 function UploadFile ($file_data) {
134 $data['uploaded_file_name'] = basename($file_data['name']);
135 $data['target_file_name'] = $file_data['uploaded_file_name'];
136 if ($settings['random_name_len'] !== false) {
138 $data['target_file_name'] = '';
139 while (strlen($data['target_file_name']) < $settings['random_name_len'])
140 $data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)];
141 if ($settings['random_name_keep_type'])
142 $data['target_file_name'] .= '.' . pathinfo($data['uploaded_file_name'], PATHINFO_EXTENSION);
143 } while (file_exists($data['target_file_name']));
145 $data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name'];
146 $data['tmp_name'] = $file_data['tmp_name'];
148 if (file_exists($data['upload_target_file'])) {
149 echo 'File name already exists' . "\n";
153 if (move_uploaded_file($data['tmp_name'], $data['upload_target_file'])) {
154 if ($settings['allow_deletion'] || $settings['allow_private'])
155 $_SESSION['upload_user_files'][] = $data['target_file_name'];
156 echo $settings['url'] . $data['target_file_name'] . "\n";
158 echo 'Error: unable to upload the file.';
164 if (isset($_FILES['file'])) {
165 if ($settings['debug']) {
166 // Displaying debug information
167 echo '<h2>Debugging information: data</h2>';
171 // Displaying debug information
172 echo '<h2>Debugging information: file</h2>';
178 if (is_array($_FILES['file'])) {
179 $file_array = DiverseArray($_FILES['file']);
180 foreach ($file_array as $file_data)
181 UploadFile($file_data);
183 UploadFile($_FILES['file']);
188 if ($settings['allow_deletion'])
189 if ($_POST['action'] === 'delete')
190 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
191 if (file_exists($_POST['target'])) {
192 unlink($_POST['target']);
193 echo 'File has been removed';
197 if ($settings['allow_private'])
198 if ($_POST['action'] === 'privatetoggle')
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 if ($_POST['target'][0] === '.') {
202 rename($_POST['target'], substr($_POST['target'], 1));
203 echo 'File has been made visible';
205 rename($_POST['target'], '.' . $_POST['target']);
206 echo 'File has been hidden';
212 function ListFiles ($dir, $exclude) {
213 $file_array = array();
215 while (false !== ($filename = readdir($dh)))
216 if (is_file($filename) && !in_array($filename, $exclude))
217 $file_array[filemtime($filename)] = $filename;
219 $file_array = array_reverse($file_array, true);
226 <meta charset="utf-8">
227 <title>strace.club</title>
228 <style media="screen">
233 font-family: sans-serif;
238 background: rgba(255, 255, 255, 0.05);
246 background: rgba(255, 255, 255, 0.075);
269 background: rgba(255, 255, 255, 0.1);
271 text-decoration: none;
276 body > ul > li > a:hover {
280 body > ul > li > a:active {
284 body > ul > li > a > span {
289 body > ul > li > form {
290 display: inline-block;
295 body > ul > li.owned {
299 body > ul > li > form > button {
301 display: inline-block;
305 background: rgba(255, 255, 255, 0.1);
309 body > ul > li > form > button:hover {
313 body > ul > li > form > button:active {
320 <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
321 Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
322 <input type="file" name="file[]" multiple required id="simpleupload-input"/>
324 <?php if ($settings['listfiles']) { ?>
327 $file_array = ListFiles($settings['uploaddir'], array('.', '..', $data['scriptname']));
328 foreach ($file_array as $mtime => $filename) {
329 $file_info = array();
331 $file_private = $filename[0] === '.';
333 if ($settings['listfiles_size'])
334 $file_info[] = FormatSize(filesize($filename));
336 if ($settings['listfiles_size'])
337 $file_info[] = date($settings['listfiles_date_format'], $mtime);
339 if ($settings['allow_deletion'] || $settings['allow_private'])
340 if (in_array(substr($filename, 1), $_SESSION['upload_user_files']) || in_array($filename, $_SESSION['upload_user_files']))
343 $file_info = implode(', ', $file_info);
345 if (strlen($file_info) > 0)
346 $file_info = ' (' . $file_info . ')';
352 if (!$file_private || $file_owner) {
353 echo "<li class=\"' . $class . '\">";
355 echo "<a href=\"$filename\" target=\"_blank\">$filename<span>$file_info</span></a>";
358 if ($settings['allow_deletion'])
359 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>';
361 if ($settings['allow_private'])
363 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>';
365 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>';
374 <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>
375 <script charset="utf-8">
376 document.getElementById('simpleupload-input').onchange = function() {
377 document.getElementById('simpleupload-form').submit();