MDL-25215, file extension should remain unchanged when using save as field in filepic...
authorDongsheng Cai <dongsheng@moodle.com>
Wed, 9 Feb 2011 08:45:14 +0000 (16:45 +0800)
committerDongsheng Cai <dongsheng@moodle.com>
Thu, 10 Feb 2011 01:40:09 +0000 (09:40 +0800)
repository/upload/lib.php

index 0edc42c..2481607 100755 (executable)
@@ -105,7 +105,21 @@ class repository_upload extends repository {
         if (empty($saveas_filename)) {
             $record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
         } else {
-            $record->filename = $saveas_filename;
+            $ext = '';
+            $match = array();
+            $filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
+            if (preg_match('/\.([a-z0-9]+)$/i', $filename, $match)) {
+                if (isset($match[1])) {
+                    $ext = $match[1];
+                }
+            }
+            $ext = !empty($ext) ? $ext : '';
+            if (preg_match('#\.(' . $ext . ')$#', $saveas_filename)) {
+                // saveas filename contains file extension already
+                $record->filename = $saveas_filename;
+            } else {
+                $record->filename = $saveas_filename . '.' . $ext;
+            }
         }
 
         if ($this->mimetypes != '*') {