phpfilelaravelfilepathpathname

Laravel - how to get path name of file


In Laravel im using this code in my controller to get files from directory:

public function galleryImages($album) {

    $images = File::allFiles('gallery/'.$album);
    return View::make('galleryimages', ['images' => $images]);
}

and in my 'galleryimages' view is:

    @foreach($images as $image)

        <img src="???">  <-- how to get file url here?

    @endforeach

Now how can I get pathName of $image variable? Var_dump of $image is returning:

object(Symfony\Component\Finder\SplFileInfo)#247 (4) {
["relativePath":"Symfony\Component\Finder\SplFileInfo":private]=> string(0) "" 
["relativePathname":"Symfony\Component\Finder\SplFileInfo":private]=> string(11) "garden5.jpg" 
["pathName":"SplFileInfo":private]=> string(25) "gallery/Album/garden5.jpg" 
["fileName":"SplFileInfo":private]=> string(11) "garden5.jpg" } 

I was trying $image->pathName, but it doesn't work.


Solution

  • You have to use the getter method

    $image->getPathname()
    

    The Symfony class extends from SplFileInfo. You can find the reference to all its methods on php.net