Skip to content

Commit 5be538c

Browse files
authored
Merge pull request #268 from viest/dev
Feat: Check for existence before opening file
2 parents 17fee0a + 556c8f9 commit 5be538c

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

kernel/excel.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ PHP_METHOD(vtiful_xls, fileName)
295295
php_stat(ZSTR_VAL(Z_STR_P(dir_path)), strlen(ZSTR_VAL(Z_STR_P(dir_path))), FS_IS_DIR, &dir_exists);
296296

297297
if (Z_TYPE(dir_exists) == IS_FALSE) {
298+
zval_ptr_dtor(&dir_exists);
298299
zend_throw_exception(vtiful_exception_ce, "Configure 'path' directory does not exist", 121);
299300
}
300301

kernel/read.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "xlswriter.h"
1414
#include "ext/date/php_date.h"
1515
#include "ext/standard/php_math.h"
16+
#include "ext/standard/php_filestat.h"
1617

1718
/* {{{ */
1819
xlsxioreader file_open(const char *directory, const char *file_name) {
@@ -23,13 +24,25 @@ xlsxioreader file_open(const char *directory, const char *file_name) {
2324
strcat(path, "/");
2425
strcat(path, file_name);
2526

27+
zval file_exists;
28+
php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
29+
30+
if (Z_TYPE(file_exists) == IS_FALSE) {
31+
efree(path);
32+
zval_ptr_dtor(&file_exists);
33+
zend_throw_exception(vtiful_exception_ce, "File not found, please check the path in the config or file name", 121);
34+
return NULL;
35+
}
36+
2637
if ((file = xlsxioread_open(path)) == NULL) {
2738
efree(path);
39+
zval_ptr_dtor(&file_exists);
2840
zend_throw_exception(vtiful_exception_ce, "Failed to open file", 100);
2941
return NULL;
3042
}
3143

3244
efree(path);
45+
zval_ptr_dtor(&file_exists);
3346
return file;
3447
}
3548
/* }}} */
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/include/skipif.inc';
6+
skip_disable_reader();
7+
?>
8+
--FILE--
9+
<?php
10+
try {
11+
$config = ['path' => './tests'];
12+
$excel = new \Vtiful\Kernel\Excel($config);
13+
14+
$excel->openFile('tutorial_not_found.xlsx');
15+
} catch (Vtiful\Kernel\Exception $exception) {
16+
var_dump($exception->getMessage());
17+
}
18+
?>
19+
--CLEAN--
20+
<?php
21+
//
22+
?>
23+
--EXPECT--
24+
string(64) "File not found, please check the path in the config or file name"

0 commit comments

Comments
 (0)