1

I am attempting to import a csv using PHPExcel into my application so that I can load the data into a table. When the file reaches 2 meg+ the code fails.

I'm running Laravel on WAMP64. The code that is failing is:

$objPHPExcel = PHPExcel_IOFactory::load(Input::file('file')->getRealPath());

The error message is:

ErrorException: file_get_contents(C:\wamp\www\imax\public): failed to open stream: Permission denied in     C:\wamp\www\imax\vendor\phpoffice\phpexcel\Classes\PHPExcel\Shared\OLERead.php:85

I know it's a size issue because the code completes properly when the file is 2048K. I can add one character to the file pushing it to 2049K and it fails. So it's not a permissions issue.

The line that fails in OLERead.php is:

// Get the file identifier
// Don't bother reading the whole file until we know it's a valid OLE file
$this->data = file_get_contents($sFileName, FALSE, NULL, 0, 8);

Wampserver 3.0.6

PHP 7.0.10

JEberlan
  • 11
  • 1
  • Have you tried uping the `memory_limit` in the php.ini file? There is also a timeout line and some other ones I would 'up' but i cant remember offhand. This may be help: https://stackoverflow.com/questions/3829403/how-to-increase-the-execution-timeout-in-php – Radmation Sep 15 '17 at 20:13
  • Memory limit was at 512M. Doubled it after I discovered the error, no change. – JEberlan Sep 18 '17 at 15:01

2 Answers2

0

It sounds like you need to up the memory allocated for PHP. You can do that at runtime or change your php.ini config file.

To up memory limit during runtime:

ini_set('memory_limit','16M'); Feel free to change the 16M to what you need.

To change it permanently:

Open your php.ini file and look for this line upload_max_filesize = 2M; and change the 2M to what you desire. (i also believe WAMP lets you edit this by right clicking the icon and choosing on of the options)

Note: You may want to just search for upload_max_filesize and leave out the = 2 M part as yours may be different.

Radmation
  • 1,486
  • 1
  • 13
  • 30
0

If the code is using OLERead, then either you're not reading a large CSV file, but a BIFF-format xls file... or you're letting PHPExcel try and identify the filetype itself.... if you know that it is a CSV file, then instantiate the CSV Reader manually rather than letting PHPExcel try and identify the file.

// Tell PHPExcel that you want to load a CSV file
$objReader = new PHPExcel_Reader_CSV();
// Load the $inputFileName to a PHPExcel Object
$objPHPExcel = $objReader->load(Input::file('file')->getRealPath());

However, if you know that you're working with CSV files, then it's more efficient to use PHP's native CSV reading function, fgetcsv()

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • I changed to the PHPExcel_Reader_CSV but it ended in a similar error. **ErrorException: fopen(C:\wamp\www\imax\public): failed to open stream: Permission denied in C:\wamp\www\imax\vendor\phpoffice\phpexcel\Classes\PHPExcel\Reader\Abstract.php:203** So different piece of code, failing with the same misleading message. I remove 1 character from the file and it works just fine. – JEberlan Sep 18 '17 at 19:39