]> git.mxchange.org Git - simple-upload.git/blobdiff - index.php
Some improvements:
[simple-upload.git] / index.php
index a5f62f569c4b994ef5f0a1e65dde5c2162cb7292..7ecb92709b15ea00de131bdaffec0c14b818d78c 100644 (file)
--- a/index.php
+++ b/index.php
                'ignores' => array('.', '..', 'LICENSE', 'README.md'),
 
                // Language code
-               'lang' => 'en_GB',
+               'lang' => 'en',
 
                // Language direction
                'lang_dir' => 'ltr',
+
+               // Remove old files?
+               'remove_old_files' => true,
        );
        // =============={ Configuration End }==============
 
+       // Is the local config file there?
+       if (file_exists('config-local.php')) {
+               // Load it then
+               include('config-local.php');
+       }
+
        // Enabling error reporting
        if ($settings['debug']) {
                error_reporting(E_ALL);
@@ -88,7 +97,7 @@
        $data['ignores'][] = $data['scriptname'];
 
        // Use canonized path
-       $data['uploaddir'] = realpath($settings['uploaddir']);
+       $data['uploaddir'] = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . $settings['uploaddir']);
 
        // Maximum upload size, set by system
        $data['max_upload_size'] = ini_get('upload_max_filesize');
 
                // 'User ID'
                if (!isset($_SESSION['upload_user_id']))
-                       $_SESSION['upload_user_id'] = rand(100000, 999999);
+                       $_SESSION['upload_user_id'] = mt_rand(100000, 999999);
 
                // List of filenames that were uploaded by this user
                if (!isset($_SESSION['upload_user_files']))
        }
 
        // Format file size
-       function FormatSize ($bytes) {
+       function formatSize ($bytes) {
                $units = array('B', 'KB', 'MB', 'GB', 'TB');
 
                $bytes = max($bytes, 0);
        }
 
        // Rotating a two-dimensional array
-       function DiverseArray ($vector) {
+       function diverseArray ($vector) {
                $result = array();
                foreach ($vector as $key1 => $value1)
                        foreach ($value1 as $key2 => $value2)
        }
 
        // Handling file upload
-       function UploadFile ($file_data) {
+       function uploadFile ($file_data) {
                global $settings;
                global $data;
                global $_SESSION;
                        do {
                                $file_data['target_file_name'] = '';
                                while (strlen($file_data['target_file_name']) < $settings['random_name_len'])
-                                       $file_data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)];
+                                       $file_data['target_file_name'] .= $settings['random_name_alphabet'][mt_rand(0, strlen($settings['random_name_alphabet']) - 1)];
                                if ($settings['random_name_keep_type'])
                                        $file_data['target_file_name'] .= '.' . pathinfo($file_data['uploaded_file_name'], PATHINFO_EXTENSION);
                        } while (file_exists($file_data['target_file_name']));
        if (isset($_FILES['file'])) {
                header('Content-type: text/plain');
                if (is_array($_FILES['file'])) {
-                       $file_array = DiverseArray($_FILES['file']);
+                       $file_array = diverseArray($_FILES['file']);
                        foreach ($file_array as $file_data)
-                               UploadFile($file_data);
+                               uploadFile($file_data);
                } else
-                       UploadFile($_FILES['file']);
+                       uploadFile($_FILES['file']);
                exit;
        }
 
        }
 
        // List files in a given directory, excluding certain files
-       function ListFiles ($dir, $exclude) {
+       function listFiles ($dir, $exclude) {
                $file_array = array();
                $dh = opendir($dir);
-                       while (false !== ($filename = readdir($dh)))
-                               if (is_file($filename) && !in_array($filename, $exclude))
-                                       $file_array[filemtime($filename)] = $filename;
+                       while (false !== ($filename = readdir($dh))) {
+                               $fqfn = $dir . DIRECTORY_SEPARATOR . $filename;
+                               if (is_file($fqfn) && !in_array($filename, $exclude))
+                                       $file_array[filemtime($fqfn)] = $filename;
+                       }
+
                ksort($file_array);
                $file_array = array_reverse($file_array, true);
                return $file_array;
        }
 
-       $file_array = ListFiles($settings['uploaddir'], $data['ignores']);
+       // Removes old files
+       function removeOldFiles ($dir) {
+               global $file_array, $settings;
+               foreach ($file_array as $file) {
+                       $fqfn = $dir . DIRECTORY_SEPARATOR . $file;
+                       if ($settings['time_limit'] < time() - filemtime($fqfn))
+                               unlink($fqfn);
+               }
+       }
+
+       $file_array = listFiles($data['uploaddir'], $data['ignores']);
 
        // Removing old files
-       foreach ($file_array as $file)
-               if ($settings['time_limit'] < time() - filemtime($file))
-                       unlink($file);
-
-       $file_array = ListFiles($settings['uploaddir'], $data['ignores']);
+       if ($settings['remove_old_files'])
+               removeOldFiles($data['uploaddir']);
 
+       $file_array = listFiles($data['uploaddir'], $data['ignores']);
+       //die(print_r($file_array, TRUE));
 ?>
 <!DOCTYPE html>
 <html lang="<?=$settings['lang']?>" dir="<?=$settings['lang_dir']?>">
                        <ul id="simpleupload-ul">
                                <?php
                                        foreach ($file_array as $mtime => $filename) {
+                                               $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $filename;
                                                $file_info = array();
                                                $file_owner = false;
                                                $file_private = $filename[0] === '.';
 
                                                if ($settings['listfiles_size'])
-                                                       $file_info[] = FormatSize(filesize($filename));
+                                                       $file_info[] = formatSize(filesize($fqfn));
 
                                                if ($settings['listfiles_size'])
                                                        $file_info[] = date($settings['listfiles_date_format'], $mtime);
                                                if (!$file_private || $file_owner) {
                                                        echo "<li class=\"' . $class . '\">";
 
-                                                       echo "<a href=\"$filename\" target=\"_blank\">$filename<span>$file_info</span></a>";
+                                                       $url = str_replace('/./', '/', sprintf('%s%s/%s', $settings['url'], $settings['uploaddir'], $filename));
+
+                                                       echo "<a href=\"$url\" target=\"_blank\">$filename<span>$file_info</span></a>";
 
                                                        if ($file_owner) {
                                                                if ($settings['allow_deletion'])
                        </ul>
                <?php } ?>
                <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>
-               <script charset="utf-8">
+               <script type="text/javascript">
+               <!--
                        var target_form = document.getElementById('simpleupload-form'),
                                target_ul = document.getElementById('simpleupload-ul'),
                                target_input = document.getElementById('simpleupload-input');
                                AddFileLi('Uploading...', '');
                                target_form.submit();
                        };
+               //-->
                </script>
        </body>
 </html>