1

I'm trying to load an excel file from 'php://memory' stream.

Here's my code:

$path = 'http://<server>/test.xls';

$temp_file = fopen('php://memory', 'w+');

$file_contents = file_get_contents($path);
fwrite($temp_file, $file_contents);

$objPHPExcel = PHPExcel_IOFactory::load($temp_file);
fclose($temp_file);

I'm getting these errors:

PHP Warning:  pathinfo() expects parameter 1 to be string, resource given in /var/resources/classes/PHPExcel/IOFactory.php on line 224
PHP Warning:  file_exists() expects parameter 1 to be a valid path, resource given in /var/resources/classes/PHPExcel/Reader/Excel2007.php on line 81
PHP Fatal error:  Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open Resource id #15 for reading! File does not exist.' in /var/resources/classes/PHPExcel/Reader/Excel2007.php:82
Stack trace:
#0 /var/resources/classes/PHPExcel/IOFactory.php(280): PHPExcel_Reader_Excel2007->canRead(Resource id #15)
#1 /var/resources/classes/PHPExcel/IOFactory.php(191): PHPExcel_IOFactory::createReaderForFile(Resource id #15)
#2 /var/www/syncDB.php(113): PHPExcel_IOFactory::load(Resource id #15)
#3 {main}
  thrown in /var/resources/classes/PHPExcel/Reader/Excel2007.php on line 82

I don't want to write the .xls file to disk. I tried to use tmpfile(), but I still get the same problem. Is there something wrong with my code? Or do I inevitably need to pass the path of a file on disk for 'PHPExcel_IOFactory::load' to work with?

It maybe worth mentioning here that 'Mark Baker's' answer on this SO question suggests that using php://memory should be possible.

Community
  • 1
  • 1
Zaxter
  • 2,939
  • 3
  • 31
  • 48
  • The error is self explanatory. `pathinfo() expects parameter 1 to be string, resource given` This is, because `PHPExcel_IOFactory::load` waiting for a path I think, and you pass a resource to it. – vaso123 Apr 07 '15 at 14:10
  • Could you please explain how I could get the path of '$temp_file = fopen('php://memory', 'w+')'? – Zaxter Apr 07 '15 at 14:14
  • 1
    I think, there is no way to do this. Or, you can create your own class, what is extends this `PHPExcel_IOFactory`, so called it `PHPExcel_IOFactoryFromMemory` and overwrite the `load` method. – vaso123 Apr 07 '15 at 14:23

0 Answers0