mercredi 19 octobre 2016

Read a .csv file into an array in Laravel

I have stored a .csv file in this directory in Laravel 5 :

/storage/app/data.csv

Inside of this function I am trying to read the contents of this file and extract a few columns from it, like name and city to an array, sample code:

use Illuminate\Support\Facades\Storage;

public function get_data() {

 $file_n = Storage::url('data.csv');
 $file = fopen($file_n, "r");
 $all_data = array();
 while ( ($data = fgetcsv($file, 200, ",")) !==FALSE {

     $name = $data[0];
     $city = $data[1];
     $all_data = $name. " ".$city;

     array_push($array, $all_data);
  }
  fclose($file);
}

I am getting an ErrorException in Controller:

fopen(/storage/data.csv): failed to open stream: No such file or directory

But the file is there. Is there a better way to do this instead?

Thanks for looking!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire