15

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!

3
  • 1
    Try with storage_path('data.csv'). Commented Oct 19, 2016 at 15:58
  • 2
    /storage/app/data.csv != /storage/data.csv Commented Oct 19, 2016 at 15:59
  • If you need to use Storage class for this, specially if you are using s3 or minio, try \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. Commented Oct 4, 2019 at 15:40

6 Answers 6

10

I figured it out by using "storage_path".

...
$filename = storage_path('/app/data.csv');
$file = fopen($filename, "r");
$all_data = array();
while ( ($data = fgetcsv($file, 200, ",")) !==FALSE ) {
    ...
}

Thanks!

Sign up to request clarification or add additional context in comments.

1 Comment

Add use Illuminate\Support\Facades\Storage; to import Storage facade. Then $storagePath = Storage::path($file);
9

Your issue is with the path as previous people pointed out.

You can use the helper function storage_path https://laravel.com/docs/5.3/helpers#method-storage-path

In your case it will be used like this:

storage_path('app/data.csv');

As a recomendation you can use this libary to work with CSV/Excel files, it's pretty easy to use: https://github.com/Maatwebsite/Laravel-Excel

Comments

7

Laravel 5.7 try this :

Storage::disk('local')->path('import.csv');

It should be good.

Comments

1

I solved this issue by adding a url key to my local disk config in config/filesystems.php like so:

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
        'url' => storage_path('app')
    ],

Now I can reference Storage::url('filename.txt') and it returns:

    /home/josh/apps/storage/app/filename.txt

Comments

1

simply, one line can do it

$csv = array_map('str_getcsv', file(Storage::path('filename.csv')));

Comments

0

Try the below

$file_n = Storage::get('data.csv');

2 Comments

This returns the csv file as a string.
This returns the csv file as a string. – You can avoid this by using $file_path = storage_path('file-name.csv'); $row = array_map("str_getcsv", file($file_path));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.