@@ -281,22 +281,20 @@ public function loadSpreadsheetFromString(string $contents): Spreadsheet
281
281
}
282
282
283
283
/**
284
- * @param resource|string $filename
284
+ * @param resource|string $file
285
285
*/
286
- private function openFileOrMemory ($ filename ): void
286
+ private function openFileOrMemory ($ file ): void
287
287
{
288
288
// Open file
289
- $ fhandle = $ this ->canRead ($ filename );
290
- if (!$ fhandle ) {
291
- throw new Exception ($ filename . ' is an Invalid Spreadsheet file. ' );
289
+ if (!$ this ->canRead ($ file )) {
290
+ throw new Exception ($ file . ' is an Invalid Spreadsheet file. ' );
292
291
}
293
292
if ($ this ->inputEncoding === self ::GUESS_ENCODING ) {
294
- $ this ->inputEncoding = self ::guessEncoding ($ filename , $ this ->fallbackEncoding );
293
+ $ this ->inputEncoding = self ::guessEncoding ($ file , $ this ->fallbackEncoding );
295
294
}
296
- $ this ->openFile ($ filename );
295
+ $ this ->openFile ($ file );
297
296
if ($ this ->inputEncoding !== 'UTF-8 ' ) {
298
- fclose ($ this ->fileHandle );
299
- $ entireFile = file_get_contents ($ filename );
297
+ $ entireFile = stream_get_contents ($ this ->fileHandle );
300
298
$ fileHandle = fopen ('php://memory ' , 'r+b ' );
301
299
if ($ fileHandle !== false && $ entireFile !== false ) {
302
300
$ this ->fileHandle = $ fileHandle ;
@@ -355,26 +353,31 @@ private function openDataUri(string $filename): void
355
353
*
356
354
* @param resource|string $file
357
355
*/
358
- public function loadIntoExisting (string $ filename , Spreadsheet $ spreadsheet ): Spreadsheet
356
+ public function loadIntoExisting ($ file , Spreadsheet $ spreadsheet ): Spreadsheet
359
357
{
360
- return $ this ->loadStringOrFile ($ filename , $ spreadsheet , false );
358
+ return $ this ->loadStringOrFile ($ file , $ spreadsheet , false );
361
359
}
362
360
363
361
/**
364
362
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
365
363
*
366
364
* @param resource|string $file
367
365
*/
368
- private function loadStringOrFile (string $ filename , Spreadsheet $ spreadsheet , bool $ dataUri ): Spreadsheet
366
+ private function loadStringOrFile ($ file , Spreadsheet $ spreadsheet , bool $ dataUri ): Spreadsheet
369
367
{
370
368
// Deprecated in Php8.1
371
369
$ iniset = $ this ->setAutoDetect ('1 ' );
372
370
373
371
// Open file
374
372
if ($ dataUri ) {
375
- $ this ->openDataUri ($ filename );
373
+ if (!is_string ($ file )) {
374
+ throw new \Exception ('$file must be an uri ' );
375
+ }
376
+ $ this ->openDataUri ($ file );
377
+ $ filename = $ file ;
376
378
} else {
377
- $ this ->openFileOrMemory ($ filename );
379
+ $ this ->openFileOrMemory ($ file );
380
+ $ filename = 'escape ' ;
378
381
}
379
382
$ fileHandle = $ this ->fileHandle ;
380
383
@@ -559,23 +562,33 @@ public function canRead($file): bool
559
562
return false ;
560
563
}
561
564
562
- fclose ($ this ->fileHandle );
565
+ rewind ($ this ->fileHandle );
563
566
564
- // Trust file extension if any
565
- $ extension = strtolower (pathinfo ($ file , PATHINFO_EXTENSION ));
566
- if (in_array ($ extension , ['csv ' , 'tsv ' ])) {
567
- return true ;
567
+ if (is_string ($ file )) {
568
+ // Trust file extension if any
569
+ $ extension = strtolower (pathinfo ($ file , PATHINFO_EXTENSION ));
570
+ if (in_array ($ extension , ['csv ' , 'tsv ' ])) {
571
+ return true ;
572
+ }
568
573
}
569
574
570
575
// Attempt to guess mimetype
571
- $ type = mime_content_type ($ file );
576
+ $ type = mime_content_type ($ this -> fileHandle );
572
577
$ supportedTypes = [
573
578
'application/csv ' ,
574
579
'text/csv ' ,
575
580
'text/plain ' ,
576
581
'inode/x-empty ' ,
577
582
];
578
583
584
+ if (is_resource ($ file )) {
585
+ // reading mime types from a stream causes sometimes different results
586
+ $ supportedTypes [] = 'application/x-empty ' ;
587
+ $ supportedTypes [] = 'text/html ' ;
588
+ }
589
+
590
+ rewind ($ this ->fileHandle );
591
+
579
592
return in_array ($ type , $ supportedTypes , true );
580
593
}
581
594
@@ -589,10 +602,13 @@ private static function guessEncodingTestNoBom(string &$encoding, string &$conte
589
602
}
590
603
}
591
604
592
- private static function guessEncodingNoBom (string $ filename ): string
605
+ /**
606
+ * @param resource|string $file
607
+ */
608
+ private static function guessEncodingNoBom ($ file ): string
593
609
{
610
+ $ contents = is_resource ($ file ) ? stream_get_contents ($ file ) : file_get_contents ($ file );
594
611
$ encoding = '' ;
595
- $ contents = file_get_contents ($ filename );
596
612
self ::guessEncodingTestNoBom ($ encoding , $ contents , self ::UTF32BE_LF , 'UTF-32BE ' );
597
613
self ::guessEncodingTestNoBom ($ encoding , $ contents , self ::UTF32LE_LF , 'UTF-32LE ' );
598
614
self ::guessEncodingTestNoBom ($ encoding , $ contents , self ::UTF16BE_LF , 'UTF-16BE ' );
@@ -613,10 +629,17 @@ private static function guessEncodingTestBom(string &$encoding, string $first4,
613
629
}
614
630
}
615
631
616
- private static function guessEncodingBom (string $ filename ): string
632
+ /**
633
+ * @param resource|string $file
634
+ */
635
+ private static function guessEncodingBom ($ file ): string
617
636
{
618
637
$ encoding = '' ;
619
- $ first4 = file_get_contents ($ filename , false , null , 0 , 4 );
638
+ if (is_resource ($ file )) {
639
+ $ first4 = stream_get_contents ($ file , 0 , 4 );
640
+ } else {
641
+ $ first4 = file_get_contents ($ file , false , null , 0 , 4 );
642
+ }
620
643
if ($ first4 !== false ) {
621
644
self ::guessEncodingTestBom ($ encoding , $ first4 , self ::UTF8_BOM , 'UTF-8 ' );
622
645
self ::guessEncodingTestBom ($ encoding , $ first4 , self ::UTF16BE_BOM , 'UTF-16BE ' );
@@ -628,11 +651,14 @@ private static function guessEncodingBom(string $filename): string
628
651
return $ encoding ;
629
652
}
630
653
631
- public static function guessEncoding (string $ filename , string $ dflt = self ::DEFAULT_FALLBACK_ENCODING ): string
654
+ /**
655
+ * @param resource|string $file
656
+ */
657
+ public static function guessEncoding ($ file , string $ dflt = self ::DEFAULT_FALLBACK_ENCODING ): string
632
658
{
633
- $ encoding = self ::guessEncodingBom ($ filename );
659
+ $ encoding = self ::guessEncodingBom ($ file );
634
660
if ($ encoding === '' ) {
635
- $ encoding = self ::guessEncodingNoBom ($ filename );
661
+ $ encoding = self ::guessEncodingNoBom ($ file );
636
662
}
637
663
638
664
return ($ encoding === '' ) ? $ dflt : $ encoding ;
0 commit comments