]> git.mxchange.org Git - simple-upload.git/blob - index.php
Added multiple upload and removed drag&drop temporarely
[simple-upload.git] / index.php
1 <?php
2         /*
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.
7
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.
12
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/>.
15         */
16
17         // ============== Configuration begin  ==============
18
19         $settings = array(
20
21                 // Directory to store uploaded files
22                 uploaddir => '.',
23
24                 // Display list uploaded files
25                 listfiles => true,
26
27                 // Allow users to delete files that they have uploaded (will enable sessions)
28                 allow_deletion => true,
29
30                 // Display file sizes
31                 listfiles_size => true,
32
33                 // Display file dates
34                 listfiles_date => true,
35
36                 // Display file dates format
37                 listfiles_date_format => 'F d Y H:i:s',
38
39                 // Randomize file names (number of 'false')
40                 random_name_len => 4,
41
42                 // Keep filetype information (if random name is activated)
43                 random_name_keep_type => true,
44
45                 // Random file name letters
46                 random_name_alphabet => 'qwertyuiopasdfghjklzxcvbnm',
47
48                 // Display debugging information
49                 debug => ($_SERVER['SERVER_NAME'] === 'localhost')
50
51         );
52
53         // ============== Configuration end  ==============
54
55     $data = array();
56
57         // Name of this file
58         $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME);
59
60         // URL to upload page
61         $data['pageurl'] = "http" . (($_SERVER['SERVER_PORT']==443) ? "s://" : "://") . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/';
62
63         // Use canonized path
64         $data['uploaddir'] = realpath($settings['uploaddir']);
65
66         // Maximum upload size, set by system
67         $data['max_upload_size'] = ini_get('upload_max_filesize');
68
69         if ($settings['allow_deletion']) {
70                 session_start();
71
72                 if (!isset($_SESSION['upload_user_id']))
73                         $_SESSION['upload_user_id'] = rand(1000, 9999);
74
75                 if (!isset($_SESSION['upload_user_files']))
76                         $_SESSION['upload_user_files'] = array();
77         }
78
79         if ($settings['debug']) {
80         // Enabling error reporting
81         error_reporting(E_ALL);
82         error_reporting(1);
83
84         // Displaying debug information
85                 echo '<h2>Debugging information: settings</h2>';
86                 echo '<pre>';
87                 print_r($settings);
88                 echo '</pre>';
89
90         // Displaying debug information
91                 echo '<h2>Debugging information: data</h2>';
92                 echo '<pre>';
93                 print_r($data);
94                 echo '</pre>';
95                 echo '</pre>';
96
97         // Displaying debug information
98                 echo '<h2>Debugging information: _SESSION</h2>';
99                 echo '<pre>';
100                 print_r($_SESSION);
101                 echo '</pre>';
102         }
103
104         function FormatSize ($bytes) {
105                 $units = array('B', 'KB', 'MB', 'GB', 'TB');
106
107                 $bytes = max($bytes, 0);
108                 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
109                 $pow = min($pow, count($units) - 1);
110
111                 $bytes /= pow(1024, $pow);
112
113                 return ceil($bytes) . ' ' . $units[$pow];
114         }
115
116         function diverse_array ($vector) {
117             $result = array();
118             foreach($vector as $key1 => $value1)
119                 foreach($value1 as $key2 => $value2)
120                     $result[$key2][$key1] = $value2;
121             return $result;
122         }
123
124         function UploadFile ($file_data) {
125                 global $settings;
126                 global $data;
127                 global $_SESSION;
128
129                 $data['uploaded_file_name'] = basename($file_data['name']);
130                 $data['target_file_name'] = $file_data['uploaded_file_name'];
131         if ($settings['random_name_len'] !== false) {
132                         do {
133                     $data['target_file_name'] = '';
134                     while (strlen($data['target_file_name']) < $settings['random_name_len'])
135                         $data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)];
136                                 if ($settings['random_name_keep_type'])
137                                         $data['target_file_name'] .= '.' . pathinfo($data['uploaded_file_name'], PATHINFO_EXTENSION);
138                         } while (file_exists($data['target_file_name']));
139         }
140                 $data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name'];
141                 $data['tmp_name'] = $file_data['tmp_name'];
142
143
144                 echo '<pre>';
145                 print_r($data);
146                 echo '</pre>';
147
148
149                 if (move_uploaded_file($data['tmp_name'], $data['upload_target_file'])) {
150                         if ($settings['allow_deletion'])
151                                 $_SESSION['upload_user_files'][] = $data['target_file_name'];
152                         echo $data['pageurl'] .  $data['upload_target_file'] . "\n";
153                         // echo 'File: <b>' . $data['uploaded_file_name'] . '</b> successfully uploaded:<br />';
154                         // echo 'Size: <b>'. number_format($_FILES['file']['size'] / 1024, 3, '.', '') .'KB</b><br />';
155                         // 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>';
156                 } else {
157                         echo 'Error: unable to upload the file.';
158                 }
159         }
160
161         if (isset($_FILES['file'])) {
162         if ($settings['debug']) {
163             // Displaying debug information
164                 echo '<h2>Debugging information: data</h2>';
165                 echo '<pre>';
166                 print_r($data);
167                 echo '</pre>';
168             // Displaying debug information
169                 echo '<h2>Debugging information: file</h2>';
170                 echo '<pre>';
171                 print_r($_FILES);
172                 echo '</pre>';
173         }
174
175                 if (is_array($_FILES['file'])) {
176                         $file_array = diverse_array($_FILES['file']);
177                         foreach ($file_array as $file_data)
178                                 UploadFile($file_data);
179                 } else
180                         UploadFile($_FILES['file']);
181                 exit;
182         }
183
184         if ($settings['allow_deletion'])
185                 if (isset($_POST))
186                         if ($_POST['action'] === 'delete')
187                                 if (in_array($_POST['target'], $_SESSION['upload_user_files']))
188                                         if (file_exists($_POST['target'])) {
189                                                 unlink($_POST['target']);
190                                                 echo 'File has been removed';
191                                                 exit;
192                                         }
193 ?>
194 <html lang="en-GB">
195         <head>
196                 <meta charset="utf-8">
197                 <title>Simple PHP Upload</title>
198         </head>
199         <body>
200                 <h1>Simple PHP Upload</h1>
201                 <p>
202                         Maximum upload size: <?php echo $data['max_upload_size']; ?>
203                 </p>
204                 <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="my-awesome-dropzone">
205                         <div class="fallback">
206                                 Choose File: <input type="file" name="file[]" multiple required /><br />
207                                 <input type="submit" value="Upload" />
208                         </div>
209                 </form>
210                 <?php if ($settings['listfiles']) { ?>
211                         <strong>Uploaded files:</strong><br />
212                         <ul>
213                                 <?php
214                                         $dh = opendir($settings['uploaddir']);
215                                         while (false !== ($filename = readdir($dh)))
216                                                 if (is_file($filename) && !in_array($filename, array('.', '..', $data['scriptname']))) {
217                                                         $file_info = array();
218
219                                                         if ($settings['listfiles_size'])
220                                                                 $file_info[] = FormatSize(filesize($filename));
221
222                                                         if ($settings['listfiles_size'])
223                                                                 $file_info[] = date($settings['listfiles_date_format'], filemtime($filename));
224
225                                                         if ($settings['allow_deletion'])
226                                                                 if (in_array($filename, $_SESSION['upload_user_files']))
227                                                                         $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>';
228
229                                                         $file_info = implode(', ', $file_info);
230
231                                                         if (strlen($file_info) > 0)
232                                                                 $file_info = ' (' . $file_info . ')';
233
234                                                         echo "<li><a href=\"$filename\">$filename</a>$file_info</li>";
235                                                 }
236                                 ?>
237                         </ul>
238                 <?php } ?>
239         </body>
240 </html>