4 * php-simple-upload - simple script to upload files to the web server
6 * Copyright (C) 2017 Antonio Ospite <ao2@ao2.it>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 require __DIR__ . '/vendor/autoload.php';
24 use Sirius\Upload\Handler as UploadHandler;
26 // Path relative to the script dir.
27 const INCOMING_DIR = 'incoming/';
29 const MAX_FILE_SIZE = '1G';
31 const ALLOWED_EXTENSIONS = [
47 if (isset($_POST['task']) && $_POST['task'] == "upload") {
48 $uploadHandler = new UploadHandler(INCOMING_DIR);
50 $uploadHandler->addRule('extension', ['allowed' => ALLOWED_EXTENSIONS], '{label} invalid file type', 'File');
51 $uploadHandler->addRule('size', ['max' => MAX_FILE_SIZE], '{label} should be less than {max}', 'File');
53 $result = $uploadHandler->process($_FILES);
54 if ($result->isValid()) {
58 catch (\Exception $e) {
64 echo "<pre>{$result->getMessages()}</pre>";
68 $iframe_parent_request_url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . INCOMING_DIR;
70 // Avoid iframe recursion.
71 if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == $iframe_parent_request_url) {
72 echo "Iframe recursion detected, use the BACK button in the browser";
78 <form method="POST" enctype="multipart/form-data">
79 <input type="file" name="filefield[]" multiple="true"/>
80 <input type="hidden" name="task" value="upload"/>
81 <input type="submit" value="Upload File"/>
84 <iframe sandbox src="<?php echo INCOMING_DIR; ?>" height="100%" width="100%" frameborder="0">
85 Your browser does not support iframes <a href="<?php echo INCOMING_DIR; ?>">click here to view the page directly.</a>