Browse Source

Feat: Check for existence before opening file

viest 5 years ago
parent
commit
c03788935b
2 changed files with 12 additions and 0 deletions
  1. 1 0
      kernel/excel.c
  2. 11 0
      kernel/read.c

+ 1 - 0
kernel/excel.c

@@ -295,6 +295,7 @@ PHP_METHOD(vtiful_xls, fileName)
     php_stat(ZSTR_VAL(Z_STR_P(dir_path)), strlen(ZSTR_VAL(Z_STR_P(dir_path))), FS_IS_DIR, &dir_exists);
 
     if (Z_TYPE(dir_exists) == IS_FALSE) {
+        zval_ptr_dtor(&dir_exists);
         zend_throw_exception(vtiful_exception_ce, "Configure 'path' directory does not exist", 121);
     }
 

+ 11 - 0
kernel/read.c

@@ -13,6 +13,7 @@
 #include "xlswriter.h"
 #include "ext/date/php_date.h"
 #include "ext/standard/php_math.h"
+#include "ext/standard/php_filestat.h"
 
 /* {{{ */
 xlsxioreader file_open(const char *directory, const char *file_name) {
@@ -23,13 +24,23 @@ xlsxioreader file_open(const char *directory, const char *file_name) {
     strcat(path, "/");
     strcat(path, file_name);
 
+    zval file_exists;
+    php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
+
+    if (Z_TYPE(file_exists) == IS_FALSE) {
+        zval_ptr_dtor(&file_exists);
+        zend_throw_exception(vtiful_exception_ce, "File not found, please check the path in the config or file name", 121);
+    }
+
     if ((file = xlsxioread_open(path)) == NULL) {
         efree(path);
+        zval_ptr_dtor(&file_exists);
         zend_throw_exception(vtiful_exception_ce, "Failed to open file", 100);
         return NULL;
     }
 
     efree(path);
+    zval_ptr_dtor(&file_exists);
     return file;
 }
 /* }}} */