diff --git a/locallib.php b/locallib.php
index d33727d3872eb90f8579f23c59a1ab1c5919e84c..1d2201baad7d5754d47c4eae3b21512e52b1d54a 100644
--- a/locallib.php
+++ b/locallib.php
@@ -296,18 +296,20 @@ function get_text_content_from_file($usercontext, $draftareaid, $keepfilename, $
 
     //check if encoding of attached file is utf-8 else convert
     $content = $file->get_content();
-    if($encoding!=null){
-        $enc=$encoding;
-    } else {
-        $enc = mb_detect_encoding($content, null, true);
-        if($enc===false){
-            throw new invalid_parameter_exception('Encoding of attached file ' . $filepath . $filename . ' could\'nt be detectet.');
+    if($attached){
+        if($encoding!=null){
+            $enc=$encoding;
+        } else {
+            $enc = mb_detect_encoding($content, null, true);
+            if($enc===false){
+                throw new invalid_parameter_exception('Encoding of attached file ' . $filepath . $filename . ' could\'nt be detectet.');
+            }
+        }
+        if($enc!=='UTF-8'){
+            $content = mb_convert_encoding($content, 'UTF-8', $enc);        
         }
     }
-    if($enc!=='UTF-8'){
-        $content = mb_convert_encoding($content, 'UTF-8', $enc);        
-    }
-
+    
     return $content;
 }