Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anucgeorge05's avatar

open and read a csv file from storage path and convert it into mysql table

if (($file = Storage :: get('/logos/ ' . $filename )) !== FALSE) {

          while ( ($data = fgetcsv($file, 1000, ",")) !==FALSE )
        {
            $csv_data = new Issue();
        $csv_data->id = $data [0];
        $csv_data->firstname = $data [1];
        $csv_data->lastname = $data [2];
        $csv_data->email = $data [3];
        $csv_data->gender = $data [4];
        $csv_data->save ();
    }
  fclose($file);
   

}

The above code gives me the following error:

"fgetcsv() expects parameter 1 to be resource, string given"

0 likes
4 replies
burlresearch's avatar

https://laravel.com/docs/master/filesystem#retrieving-files

The get method may be used to retrieve the contents of a file. The raw string contents of the file will be returned by the method.

but, fgetcsv reads from a

handle: A valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen().

You may want to look at:

http://php.net/manual/en/function.str-getcsv.php

or perhaps at using the more powerful:

http://csv.thephpleague.com/

1 like
rifqi96's avatar

Hi, I know it's been quite some time. But have you figured out the solution? I'm facing the same case, I want to convert a csv file that's stored in the storage (cloud storage in my case) to json. I'm aware fopen() is for local file, but how do I sort of download the file from storage just to allow the file gets read by fopen() ?

burlresearch's avatar

Again, break it down. Sounds like you're talking about 2 or 3 different things here.

  1. open the file for reading. Check out https://laravel.com/docs/master/filesystem if you're not using it already
  2. parse the CSV. For this you can use one of the suggestions in my first response
  3. convert to JSON (or Object, or whatever). Likely at this stage you'll have a PHP iterable that you convert or use. If you need, perhaps json_encode() would help you?

Depending on what you're reading, you may want to be feeding this straight into your DB via Eloquent models.

Please or to participate in this conversation.