Hey guys. I am having an issue with an excel-reader script for php. I was able to read a previous smaller excel file of the same format. But now when I try and use the bigger file (more rows) it gives a

Return Code: 1
The filename is not readable

error.

Here is my code for the reading process:

  require_once("Excel/excel_reader2.php");

  $data = new Spreadsheet_Excel_Reader();
  $data->read($file);

  $serial_used = array(); 
  $serial_active = array();  

  $count = 1;  

  while($data->val($count,'L') != '')  { 
    if ($data->val($count,'L')=='Card is Active')  {
      array_push($serial_active, $data->val($count,'A'));
    }
    else  
array_push($serial_used, $data->val($count,'A')); $count++; }

    Please guys. Should I give more info maybe? Someone must've used a php excel-reader script before?

      Well the code you posted either doesn't show where the value of $file is set, or it's not being set. Please include all applicable code. You can also try this before you attempt to read the file to ensure that it exists:

      if(!file_exists($file)) die('File could not be found.');

        Well I submit the excel file via form and like I said, the previous smaller excel file was actually processed.

        Here is the form code:

        <form name="form3" enctype="multipart/form-data" method="post" action="upload_file.php" target="_blank">
                <label>
                  <input type="file" name="excel_file" id="excel_file">
                  </label>
                <label>
                  <input type="submit" name="excel_submit" id="excel_submit" value="Submit">
                </label>
        
          </form>

        Here is upload_file.php:

        <?php
          require_once("upload_excel.php");
        
          if ($_FILES["excel_file"]["error"] > 0)  {
            echo "Return Code: " . $_FILES["excel_file"]["error"] . "<br />";
          }
          else  {
            echo "Upload: " . $_FILES["excel_file"]["name"] . "<br />";
            echo "Type: " . $_FILES["excel_file"]["type"] . "<br />";
            echo "Size: " . ($_FILES["excel_file"]["size"] / 1024) . " Kb<br />";
            echo "Temp file: " . $_FILES["excel_file"]["tmp_name"] . "<br />";
          }
        
        upload_excel($_FILES["excel_file"]["tmp_name"]);
        
        ?>
        

        And here is upload_excel.php:

        <?php
        function upload_excel($file)  {
        
          require_once("Excel/excel_reader2.php");
        
          $data = new Spreadsheet_Excel_Reader();
          $data->read($file);
        
          $serial_used = array(); 
          $serial_active = array();  
        
          $count = 1;  
        
          while($data->val($count,'L') != '')  { 
            if ($data->val($count,'L')=='Card is Active')  {
              array_push($serial_active, $data->val($count,'A'));
            }
            else  
        array_push($serial_used, $data->val($count,'A')); $count++; } //print_r($serial_active); //print_r($serial_used); $con = mysql_connect("host","use","pass"); if(!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('PepBase',$con); foreach($serial_used as $serial_used2) { mysql_query("UPDATE Vodacom SET Status='used' WHERE serialNR='$serial_used2'"); } foreach($serial_active as $serial_active2) { mysql_query("UPDATE Vodacom SET Status='active' WHERE serialNR='$serial_active2'"); } mysql_close($con); } ?>

          Do you have a limit on the maximum size of a file upload? (echo ini_get('upload_max_filesize')) that could be smaller than the size of the file you're trying to upload? More generally:

              echo "Upload: " . $_FILES["excel_file"]["name"] . "<br />";
              echo "Type: " . $_FILES["excel_file"]["type"] . "<br />";
              echo "Size: " . ($_FILES["excel_file"]["size"] / 1024) . " Kb<br />";
              echo "Temp file: " . $_FILES["excel_file"]["tmp_name"] . "<br />";
          

          These are giving the correct values?

            Okay so, I changed the upload_max_filesize but it didn't help. When I remove a lot of the rows in the xls file it works. I think the excel-reader class file must have some max row setting as well...

              Oh no wait, I forgot to restart apache. So now it works but I get this now: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 42 bytes) in /var/www/vouchers/final/Vodacom/Excel/excel_reader2.php on line 83

              My memory_limit = 128M in the php.ini though. That's supposed to be fine!

                Oh well, I changed it to 256MB, its quite a CRAZY huge process I am running and my MySQL drivers aren't the best in the world 🙂...

                It works for now.

                  Niel Roos;10952581 wrote:

                  My memory_limit = 128M in the php.ini though. That's supposed to be fine!

                  You could try to conserve memory by using a for() loop and reading and processing the spreadsheet row-by-row (similar to what you'd do for a MySQL result set) instead.

                  Also, looking at the documentation, it appears as though you might be able to conserve memory simply by telling the class to throw away 'extended data' by passing boolean FALSE as the second parameter of the constructor:

                  Or conserve memory for large worksheets by not storing the extended information about cells like fonts, colors, etc.

                  $data = new Spreadsheet_Excel_Reader("test.xls",false);

                  EDIT: Either way, don't forget to mark this thread resolved (if it is) using the link on the Thread Tools menu.

                    Write a Reply...