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!
storage_path('data.csv')./storage/app/data.csv!=/storage/data.csv\Storage::disk('local_temp')->getDriver()->getAdapter()->applyPathPrefix($this->fileName);to get the full file path. This is solution for Laravel 5.1, for Laravel 5.2 and up::url()function would work. for Laravel 5.7 and up::path()function would would work.