Read file data line by line

I tried the following code :

    $contents = File::get(storage_path('files\In\/'.$filename));
     foreach($contents as $line) {
                        echo $line. "working \n";
    }

Here $contents returning string not array.

Error :

"Invalid argument supplied for foreach()"

when I var_dump data it returns the following result :

string 'A, B, V<B ,B b B a sdasd' (length=31)

Input file attached. enter image description here

1 answer

  • answered 2018-07-11 06:24 Insanity Geek

    Solved by this :

    Seems that File::get is a wrapper for file_get_contents() which returns a string. You probably want $contents = file(storage_path('files\In/'.$filename)); See php.net/manual/function.file.php

    Just need to use the following code :
    $contents = file(storage_path('files\In/'.$filename));