@@ -276,20 +276,18 @@ public function loadSpreadsheetFromString(string $contents): Spreadsheet
276
276
return $ this ->loadStringOrFile ('data://text/plain, ' . urlencode ($ contents ), $ spreadsheet , true );
277
277
}
278
278
279
- private function openFileOrMemory (string $ filename ): void
279
+ private function openFileOrMemory ($ file ): void
280
280
{
281
281
// Open file
282
- $ fhandle = $ this ->canRead ($ filename );
283
- if (!$ fhandle ) {
284
- throw new Exception ($ filename . ' is an Invalid Spreadsheet file. ' );
282
+ if (!$ this ->canRead ($ file )) {
283
+ throw new Exception ($ file . ' is an Invalid Spreadsheet file. ' );
285
284
}
286
285
if ($ this ->inputEncoding === self ::GUESS_ENCODING ) {
287
- $ this ->inputEncoding = self ::guessEncoding ($ filename , $ this ->fallbackEncoding );
286
+ $ this ->inputEncoding = self ::guessEncoding ($ file , $ this ->fallbackEncoding );
288
287
}
289
- $ this ->openFile ($ filename );
288
+ $ this ->openFile ($ file );
290
289
if ($ this ->inputEncoding !== 'UTF-8 ' ) {
291
- fclose ($ this ->fileHandle );
292
- $ entireFile = file_get_contents ($ filename );
290
+ $ entireFile = stream_get_contents ($ this ->fileHandle );
293
291
$ fileHandle = fopen ('php://memory ' , 'r+b ' );
294
292
if ($ fileHandle !== false && $ entireFile !== false ) {
295
293
$ this ->fileHandle = $ fileHandle ;
@@ -346,24 +344,26 @@ private function openDataUri(string $filename): void
346
344
/**
347
345
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
348
346
*/
349
- public function loadIntoExisting (string $ filename , Spreadsheet $ spreadsheet ): Spreadsheet
347
+ public function loadIntoExisting ($ file , Spreadsheet $ spreadsheet ): Spreadsheet
350
348
{
351
- return $ this ->loadStringOrFile ($ filename , $ spreadsheet , false );
349
+ return $ this ->loadStringOrFile ($ file , $ spreadsheet , false );
352
350
}
353
351
354
352
/**
355
353
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
356
354
*/
357
- private function loadStringOrFile (string $ filename , Spreadsheet $ spreadsheet , bool $ dataUri ): Spreadsheet
355
+ private function loadStringOrFile ($ file , Spreadsheet $ spreadsheet , bool $ dataUri ): Spreadsheet
358
356
{
359
357
// Deprecated in Php8.1
360
358
$ iniset = $ this ->setAutoDetect ('1 ' );
361
359
362
360
// Open file
363
361
if ($ dataUri ) {
364
- $ this ->openDataUri ($ filename );
362
+ $ this ->openDataUri ($ file );
363
+ $ filename = $ file ;
365
364
} else {
366
- $ this ->openFileOrMemory ($ filename );
365
+ $ this ->openFileOrMemory ($ file );
366
+ $ filename = 'escape ' ;
367
367
}
368
368
$ fileHandle = $ this ->fileHandle ;
369
369
@@ -548,23 +548,33 @@ public function canRead($file): bool
548
548
return false ;
549
549
}
550
550
551
- fclose ($ this ->fileHandle );
551
+ rewind ($ this ->fileHandle );
552
552
553
- // Trust file extension if any
554
- $ extension = strtolower (pathinfo ($ file , PATHINFO_EXTENSION ));
555
- if (in_array ($ extension , ['csv ' , 'tsv ' ])) {
556
- return true ;
553
+ if (is_string ($ file )) {
554
+ // Trust file extension if any
555
+ $ extension = strtolower (pathinfo ($ file , PATHINFO_EXTENSION ));
556
+ if (in_array ($ extension , ['csv ' , 'tsv ' ])) {
557
+ return true ;
558
+ }
557
559
}
558
560
559
561
// Attempt to guess mimetype
560
- $ type = mime_content_type ($ file );
562
+ $ type = mime_content_type ($ this -> fileHandle );
561
563
$ supportedTypes = [
562
564
'application/csv ' ,
563
565
'text/csv ' ,
564
566
'text/plain ' ,
565
567
'inode/x-empty ' ,
566
568
];
567
569
570
+ if (is_resource ($ file )) {
571
+ // reading mime types from a stream causes sometimes different results
572
+ $ supportedTypes [] = 'application/x-empty ' ;
573
+ $ supportedTypes [] = 'text/html ' ;
574
+ }
575
+
576
+ rewind ($ this ->fileHandle );
577
+
568
578
return in_array ($ type , $ supportedTypes , true );
569
579
}
570
580
0 commit comments