edit: implement filename storage in upload include
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 11 Jul 2018 19:12:12 +0000 (21:12 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 14 Jul 2018 11:47:55 +0000 (13:47 +0200)
edit/index.php
login/edit.php
upload.inc.php

index 6f4dc4d495aa1fc45d3a68db6f68e6107e100211..cd222163a5a3615a7ad39193a0bcf218e471ae56 100644 (file)
@@ -7,23 +7,15 @@ if (empty($User['admin']))
 if ($_FILES) {
        $response = ['uploaded' => 0];
        try {
-               $img = @$_FILES['upload'];
-               $response['fileName'] = $img['name'];
-               if (!$img or $img['error'] !== UPLOAD_ERR_OK)
-                       throw new Exception('bestand niet goed ontvangen: '.$img['error']);
-
+               require_once('upload.inc.php');
                $datadir = implode('/', ['data', date('Y')]);
                if ($Args) $datadir .= $Args;
-               if (!file_exists($datadir) and !@mkdir($datadir, 0777, TRUE)) {
-                       throw new Exception("bestand kon niet geplaatst worden in $datadir");
-               }
-
-               $target = $datadir.'/'.$img['name'];
-               $response['url'] = str_replace('%2F', '/', urlencode($target));
-               if (!@move_uploaded_file($img['tmp_name'], $target)) {
-                       throw new Exception('bestand kon niet worden opgeslagen');
+               $target = userupload(@$_FILES['upload'], $datadir);
+               if ($target) {
+                       $response['fileName'] = $_FILES['upload']['name'];
+                       $response['url'] = str_replace('%2F', '/', urlencode($target));
+                       $response['uploaded']++;
                }
-               $response['uploaded']++;
        }
        catch (Exception $e) {
                $response['error'] = ['message' => $e->getMessage()];
index 101244afbb42aab0e5d55a47072f2d879078e57c..ad9dbda68faa8f378081dfcbf2bee041bb74190a 100644 (file)
@@ -129,12 +129,12 @@ if ($_POST) {
                }
                try {
                        require_once('upload.inc.php');
-                       $target = userupload($val, $cols[$col]['target']);
+                       $target = userupload($val, NULL, $cols[$col]['target']);
                        if (!$target) continue;
                        $cols[$col]['value'] = '';
                }
                catch (Exception $e) {
-                       $colwarn[$col] = $e->getMessage();
+                       $colwarn[$col] = ucfirst($e->getMessage()).'.';
                }
        }
 
index aa245588d69aae98dfd0e2a8180ded4de0b02362..f1018c2417770f09701d67970e1617ae5a4c3ca6 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-function userupload($input, $target)
+function userupload($input, $target = NULL, $filename = NULL)
 {
        switch ($input['error']) {
        case UPLOAD_ERR_OK:
@@ -7,10 +7,24 @@ function userupload($input, $target)
        case UPLOAD_ERR_NO_FILE:
                return; # current
        default:
-               throw new Exception("Afbeelding niet goed ontvangen.");
+               throw new Exception('bestand niet goed ontvangen: '.$input['error']);
        }
+
+       if (isset($target)) {
+               if (!file_exists($target) and !@mkdir($target, 0777, TRUE)) {
+                       throw new Exception("bestand kon niet geplaatst worden in $target");
+               }
+               $target .= '/';
+       }
+       if (isset($filename)) {
+               $target .= $filename;
+       }
+       else {
+               $target .= $input['name'];
+       }
+
        if (!@move_uploaded_file($input['tmp_name'], $target)) {
-               throw new Exception("Fout bij opslaan.");
+               throw new Exception('bestand kon niet worden opgeslagen');
        }
 
        foreach (@glob('thumb/*/') as $thumbres) {