Are you sure $files has any values? You can check that by just doing dd($files) before return view(). It will die and dump the given variable. If that is empty, then that is the reason you don't see anything ;)
Nov 5, 2018
7
Level 1
List files in a directory
Hello,
After upload a file on the server, in /storage/app/public/uploads, I would like, on the same view that the send form, show a list of all the files in this directory.
I have this in my controller :
public function uploadFile(){
$allFiles = Storage::allFiles('/public/uploads');
$files = array();
foreach ($allFiles as $file) {
$files[] = $this->fileInfo(pathinfo(public_path() . '/public/uploads' . $file));
}
return view('uploadfile', compact('files'));
}
public function fileInfo($filePath)
{
$file = array();
$file['name'] = $filePath['filename'];
$file['extension'] = $filePath['extension'];
$file['size'] = filesize($filePath['dirname'] . '/' . $filePath['basename']);
return $file;
}
And in the view :
@foreach ($files as $file)
<div class="row">
<div class="col-sm-6">
<p>{{ $file['name'] }}</p>
</div>
<div class="col-sm-2">{{ $file['extension'] }}</div>
<div class="col-sm-2">{{ $file['size'] }}</div>
<div class="col-sm-2">
<a href="" class="btn btn-primary btn-block">Download</a>
</div>
</div>
@endforeach
There's no error, but nothing appear on the view.
What did I missed ? Thanks,
ANDRE Ani
Please or to participate in this conversation.