Without Laravel I can use simple code to read text file by line:
$file = fopen("whatever/file.txt", "r") or exit("Unable to open file!");
while(!feof($file)) {
echo fgets($file). "<br>";
}
fclose($file);
With Laravel this simple thing becomes overwhelming due to local file storage location.
I.e. I can get file contents with Storage::get('whatever/file.txt')
method, but how to get just a file and then read it in a loop?
I've tried to use File::get('whatever/file.txt')
method but get an error: File does not exist at path
.
How to read file from local storage (not public) line by line with Laravel?
You can get your file like this:
$file = fopen(storage_path("whatever/file.txt"), "r");
This will result in a path similar to this
'/var/www/storage/whatever/file.txt'
or '/var/www/foo/storage/whatever/file.txt'
if you are serving multiple websites from the same server, it will depend on your setup, but you get the gist of it. Then you can read your file;
while(!feof($file)) {
echo fgets($file). "<br>";
}
fclose($file);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With