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,
31 listfiles_size => true,
34 listfiles_date => true,
36 // Display file dates format
37 listfiles_date_format => 'F d Y H:i:s',
39 // Randomize file names (number of 'false')
42 // Keep filetype information (if random name is activated)
43 random_name_keep_type => true,
45 // Random file name letters
46 random_name_alphabet => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
48 // Display debugging information
49 debug => ($_SERVER['SERVER_NAME'] === 'localhost')
53 // ============== Configuration end ==============
58 $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME);
61 $data['pageurl'] = "http" . (($_SERVER['SERVER_PORT']==443) ? "s://" : "://") . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/';
64 $data['uploaddir'] = realpath($settings['uploaddir']);
66 // Maximum upload size, set by system
67 $data['max_upload_size'] = ini_get('upload_max_filesize');
69 if ($settings['allow_deletion']) {
72 if (!isset($_SESSION['upload_user_id']))
73 $_SESSION['upload_user_id'] = rand(1000, 9999);
75 if (!isset($_SESSION['upload_user_files']))
76 $_SESSION['upload_user_files'] = array();
79 if ($settings['debug']) {
82 // Enabling error reporting
83 error_reporting(E_ALL);
86 // Displaying debug information
87 echo '<h2>Debugging information: settings</h2>';
92 // Displaying debug information
93 echo '<h2>Debugging information: data</h2>';
99 // Displaying debug information
100 echo '<h2>Debugging information: _SESSION</h2>';
106 function FormatSize ($bytes) {
107 $units = array('B', 'KB', 'MB', 'GB', 'TB');
109 $bytes = max($bytes, 0);
110 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
111 $pow = min($pow, count($units) - 1);
113 $bytes /= pow(1024, $pow);
115 return ceil($bytes) . ' ' . $units[$pow];
118 function DiverseArray ($vector) {
120 foreach($vector as $key1 => $value1)
121 foreach($value1 as $key2 => $value2)
122 $result[$key2][$key1] = $value2;
126 function UploadFile ($file_data) {
131 $data['uploaded_file_name'] = basename($file_data['name']);
132 $data['target_file_name'] = $file_data['uploaded_file_name'];
133 if ($settings['random_name_len'] !== false) {
135 $data['target_file_name'] = '';
136 while (strlen($data['target_file_name']) < $settings['random_name_len'])
137 $data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)];
138 if ($settings['random_name_keep_type'])
139 $data['target_file_name'] .= '.' . pathinfo($data['uploaded_file_name'], PATHINFO_EXTENSION);
140 } while (file_exists($data['target_file_name']));
142 $data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name'];
143 $data['tmp_name'] = $file_data['tmp_name'];
145 if (file_exists($data['upload_target_file'])) {
146 echo 'File name already exists' . "\n";
156 if (move_uploaded_file($data['tmp_name'], $data['upload_target_file'])) {
157 if ($settings['allow_deletion'])
158 $_SESSION['upload_user_files'][] = $data['target_file_name'];
159 echo $data['pageurl'] . $data['upload_target_file'] . "\n";
160 // echo 'File: <b>' . $data['uploaded_file_name'] . '</b> successfully uploaded:<br />';
161 // echo 'Size: <b>'. number_format($_FILES['file']['size'] / 1024, 3, '.', '') .'KB</b><br />';
162 // echo 'File /URL: <b><a href="http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$data['upload_target_file'].'">http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$data['upload_target_file'].'</a></b>';
164 echo 'Error: unable to upload the file.';
168 if (isset($_FILES['file'])) {
169 if ($settings['debug']) {
170 // Displaying debug information
171 echo '<h2>Debugging information: data</h2>';
175 // Displaying debug information
176 echo '<h2>Debugging information: file</h2>';
182 if (is_array($_FILES['file'])) {
183 $file_array = DiverseArray($_FILES['file']);
184 foreach ($file_array as $file_data)
185 UploadFile($file_data);
187 UploadFile($_FILES['file']);
191 if ($settings['allow_deletion'])
193 if ($_POST['action'] === 'delete')
194 if (in_array($_POST['target'], $_SESSION['upload_user_files']))
195 if (file_exists($_POST['target'])) {
196 unlink($_POST['target']);
197 echo 'File has been removed';
203 <meta charset="utf-8">
204 <title>Simple PHP Upload</title>
207 <h1>Simple PHP Upload</h1>
209 Maximum upload size: <?php echo $data['max_upload_size']; ?>
211 <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="my-awesome-dropzone">
212 <div class="fallback">
213 Choose File: <input type="file" name="file[]" multiple required /><br />
214 <input type="submit" value="Upload" />
217 <?php if ($settings['listfiles']) { ?>
218 <strong>Uploaded files:</strong><br />
221 $dh = opendir($settings['uploaddir']);
222 while (false !== ($filename = readdir($dh)))
223 if (is_file($filename) && !in_array($filename, array('.', '..', $data['scriptname']))) {
224 $file_info = array();
226 if ($settings['listfiles_size'])
227 $file_info[] = FormatSize(filesize($filename));
229 if ($settings['listfiles_size'])
230 $file_info[] = date($settings['listfiles_date_format'], filemtime($filename));
232 if ($settings['allow_deletion'])
233 if (in_array($filename, $_SESSION['upload_user_files']))
234 $file_info[] = '<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>';
236 $file_info = implode(', ', $file_info);
238 if (strlen($file_info) > 0)
239 $file_info = ' (' . $file_info . ')';
241 echo "<li><a href=\"$filename\">$filename</a>$file_info</li>";