commit 55154c7fb0522d9c2c28b377613767fa4232492b
parent 7544233accd8763d61405a5f8590934072325708
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Fri, 25 Oct 2024 14:55:55 +0200
fix: return empty string if file could not be read
Diffstat:
1 file changed, 24 insertions(+), 9 deletions(-)
diff --git a/lib/read.php b/lib/read.php
@@ -10,17 +10,32 @@ function read_note(
     $dirpath = "./data/" . $user . "/" . $category;
     $filepath = $dirpath . "/" . $filename;
 
-    /* create file-handle */
-    $file = fopen(
-        $filepath,
-        "r"
-    );
+    /* get filesize */
+    $filesize = filesize($filepath);
 
-    /* Read from file */
-    $content = fread($file, filesize($filepath));
+    if ($filesize > 0) {
 
-    /* return content.If reading fails, returns "false" */
-    return $content;
+        /* create file-handle */
+        $file = fopen(
+            $filepath,
+            "r"
+        );
+
+        /* Read from file */
+        $content = fread($file, $filesize);
+
+    } else {
+
+        $content = false;
+
+    }
+
+    /* return content.If reading fails, returns "" */
+    if ($content === false) {
+        return "";
+    } else {
+        return $content;
+    }
 
 }