Cant load file from a variable

Topics: Developer Forum, User Forum
Jan 24, 2012 at 4:26 PM
Edited Jan 24, 2012 at 4:32 PM

Hi all. I think its a silly one.

I am trying to load an xls from a variable

While i can run this code      

$reader = PHPExcel_IOFactory::load("/tmp/dir/uploads/upload.xls")

this one cant load

      $reader = PHPExcel_IOFactory::load("\"{$filename}\"");

or $filename

or any other combination like "{$filename}" etc

$filename is a string $filename="/tmp/dir/uploads/upload.xls";

and throws a:

Fatal error: Uncaught exception 'Exception' with message 'Could not open "" for reading! File does not exist.' in ../PHPExcel/PHPExcel/Reader/Excel5.php:509 Stack trace: #0 ../php/PHPExcel/PHPExcel/IOFactory.php(271): PHPExcel_Reader_Excel5->canRead('""') #1 ../PHPExcel/PHPExcel/IOFactory.php(192): PHPExcel_IOFactory::createReaderForFile('""') #2 /opt/lampp/htdocs/test.php(169): PHPExcel_IOFactory::load('""') #3 {main} thrown in ../PHPExcel/PHPExcel/Reader/Excel5.php on line 509

 

I think i am missing something really obvious! Any help will be greatly appreciated!

Tia

Coordinator
Jan 24, 2012 at 4:31 PM

This isn't a PHPExcel bug, this is illogical coding

\"{$filename}\"

with $filename = /tmp/dir/uploads/upload.xls

gives an actual filename of "/tmp/dir/uploads/upload.xls"

clearly your filename doesn't have the quotes in it, so file isn't found... I'm not aware of any operating platform where that would be a valid filename either

 

so get rid of the \"

Jan 24, 2012 at 4:52 PM

Well thanks for replying.

It appears that it cant find the $filename var cause its a php session var which stores an uploaded file.

i am running the if(move_uploaded_file()) routine and i get true,

i dump the session var ,it is a string pointing at the destination, the file is there but phpexcel cant read it.Holly cow where is my fault!

Sorry for not being honest from the beginning.

I am trying to debug it all day long and i am gonna have a nervous breakdown.

Jan 24, 2012 at 5:25 PM

how about reading Marks answer? ;)

 

try printing $filename to see if its contents are correct and change your loading call to:

if ( is_file($filename) ){
  $reader = PHPExcel_IOFactory::load($filename);
} else {
  printf("File not found: %s\n", $filename);
}

Jan 24, 2012 at 6:02 PM

sorry for wasting your time guys.Its obviously my fault .It appears that something is wrong with my sessions.

I am running either memcache or files directive in

php.ini session save handler

and while i can print the session var in the uploaded page in the phpexcel reading script page i cant get it .Thanks

Coordinator
Jan 24, 2012 at 6:29 PM
Edited Jan 24, 2012 at 6:31 PM

@madprof - forget your sessions, that may be a problem, but it isn't the very obvious one in the code you've posted

$reader = PHPExcel_IOFactory::load("\"{$filename}\"");

Each \" in the above line is adding an actual quote (") character to the beginning and end of the filename that you're trying to load. Your filename does not contain quotes, so why add them? Just do:

$reader = PHPExcel_IOFactory::load($filename);


$filename may not be the correct value, but get rid of the obvious code error first, then look at the string contained in $filename.

Jan 24, 2012 at 6:46 PM

I tried every possible combination of the SESSION['filename'] variable the one you quoted is only one of the examples, i now am going to test in windows to see how thinks will work. PHPexcel is an amazing and pretty useful library thanks for sharing. This is my only bug its a project for my dissertation but i am gonna run it on a shared hosting after hardening it a bit. Project went out really good.On a later stage i am gonna post  a function i made to check the excel file for malicious code and would need some help cause this function renders a file that puts after checking it all the contents in the first column.Uses fgets the only one that works. I 've put some malicious code in the uploaded excel file it does no harm but i obviously aint gonna run the site live without checking the file prior the reading.

With respect, to you and all

George.