2727
2828use function chr ;
2929use function count ;
30+ use function dbase_close ;
31+ use function dbase_create ;
32+ use function dbase_delete_record ;
33+ use function dbase_open ;
34+ use function dbase_pack ;
3035use function extension_loaded ;
3136use function fclose ;
3237use function feof ;
@@ -76,6 +81,8 @@ class ShapeFile
7681 /** @var array<int, ShapeRecord> */
7782 public array $ records = [];
7883
84+ private bool $ allowNoDbf = false ;
85+
7986 /**
8087 * Checks whether dbase manipulations are supported.
8188 */
@@ -101,6 +108,11 @@ public function __construct(
101108 ) {
102109 }
103110
111+ public function setAllowNoDbf (bool $ allowNoDbf ): void
112+ {
113+ $ this ->allowNoDbf = $ allowNoDbf ;
114+ }
115+
104116 /**
105117 * Loads shapefile and dbase (if supported).
106118 *
@@ -304,7 +316,16 @@ public function getIndexFromDBFData(string $field, string $value): int
304316 */
305317 private function loadDBFHeader (): array
306318 {
307- $ dbfFile = fopen ($ this ->getFilename ('.dbf ' ), 'r ' );
319+ if (! self ::supportsDbase ()) {
320+ return [];
321+ }
322+
323+ $ dbfName = $ this ->getFilename ('.dbf ' );
324+ if (! file_exists ($ dbfName )) {
325+ return [];
326+ }
327+
328+ $ dbfFile = fopen ($ dbfName , 'r ' );
308329 if ($ dbfFile === false ) {
309330 return [];
310331 }
@@ -390,9 +411,7 @@ private function loadHeaders(): bool
390411 $ this ->boundingBox ['mmin ' ] = Util::loadData ('d ' , $ this ->readSHP (8 ));
391412 $ this ->boundingBox ['mmax ' ] = Util::loadData ('d ' , $ this ->readSHP (8 ));
392413
393- if (self ::supportsDbase ()) {
394- $ this ->dbfHeader = $ this ->loadDBFHeader ();
395- }
414+ $ this ->dbfHeader = $ this ->loadDBFHeader ();
396415
397416 return true ;
398417 }
@@ -613,6 +632,10 @@ private function openDBFFile(): bool
613632
614633 $ dbfName = $ this ->getFilename ('.dbf ' );
615634 if (! is_readable ($ dbfName )) {
635+ if ($ this ->allowNoDbf ) {
636+ return true ;
637+ }
638+
616639 $ this ->setError (sprintf ('It wasn \'t possible to find the DBase file "%s" ' , $ dbfName ));
617640
618641 return false ;
0 commit comments