Nikko Dela Cruz
Nikko Dela Cruz

Reputation: 42

read csv file with wordpress

How to read a csv file with wordpress, i tried the code below but it's not working:

     require_once("PHPexcel/PHPExcel.php");
     $file = "sample.csv"; 
     $excelReader = PHPExcel_IOFactory::createReaderForFile($file);
     $excelObj = $excelReader->load($file);
     $workSheet = $excelObj->getSheet(0);
     $lastRow = $workSheet->getHighestRow();

     for ($row=2; $row <=$lastRow ; $row++) { 
        $col1 = $workSheet->getCell('A'.$row)->getValue();
        var_dump($col1);
     }

I tried this in separated php file and it works, but when i put it in wordpress, it's not working anymore and i got an error: Fatal error: Uncaught PHPExcel_Reader_Exception: Could not open sample.csv for reading! File does not exist.

Upvotes: 1

Views: 6656

Answers (3)

Darkcoder
Darkcoder

Reputation: 858

for me this worked . and remember one thing

save your csv into CSV (MS-DOS) (*.csv) format and then try following code

enter image description here

And here example of my csv data

NAME;ADDRESS;ZIP;CITY;TELEPHONE;SPONSOR;URL

global $wpdb;
$filename=$_FILES["file"]["tmp_name"];    
if($_FILES["file"]["size"] > 0)
{
  $handle = fopen($filename, "r") or die("Error opening file");
  $i = 0;
  while(($line = fgetcsv($handle)) !== FALSE) {
      if($i == 0) {
          $c = 0;
          foreach($line as $col) {
              $cols[$c] = $col;
              $c++;
          }
      } else if($i > 0) {
          $c = 0;
          foreach($line as $col) {
              $client_data[$i][$cols[$c]] = $col;
              $c++;
          }
      }
      $i++;
  }
  fclose($handle);
  // var_dump($cols);
    $table_name = $wpdb->prefix . 'rlist';
  foreach ($client_data as $client_row){
    if (is_array($client_row)) {
      foreach ($client_row as $value) {
        $savedata = explode(";", $value);
        if(!empty($savedata)){
            $savedata[0] = ($savedata[0]) ? $savedata[0] : null;
            $savedata[1] = ($savedata[1]) ? $savedata[1] : null;
            $savedata[2] = ($savedata[2]) ? $savedata[2] : null;
            $savedata[3] = ($savedata[3]) ? $savedata[3] : null;
            $savedata[4] = ($savedata[4]) ? $savedata[4] : null;
            $savedata[5] = ($savedata[5]) ? $savedata[5] : null;
            $savedata[6] = ($savedata[6]) ? $savedata[6] : null;
          $wpdb->insert(
              $table_name,
              array(
                  'NAME' => $savedata[0],
                  'ADDRESS' => $savedata[1],
                  'ZIP' =>$savedata[2],
                  'CITY' =>$savedata[3],
                  'TELEPHONE' =>$savedata[4],
                  'SPONSOR' =>$savedata[5],
                  'URL'=> $savedata[6]
              )
          );

        }
      }
    }
  }
  fclose($file);

and here is working output of this code. enter image description here

Upvotes: 1

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

change following line

$file = "sample.csv"; 

to

$file = ABSPATH."/sample.csv"; 

You need to provide full absolute path of your csv.

Thanks!

Upvotes: -1

Rajkumar Gour
Rajkumar Gour

Reputation: 1159

Place the smaple.csv file in your wordpress root. Put the PHPexcel library folder in your theme folder and place the above code in your functions.php

Upvotes: -1

Related Questions