Laravel - how to get path name of file
Asked Answered
O

2

6

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.

Orcus answered 14/12, 2014 at 12:19 Comment(0)
G
12

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

Gibbous answered 14/12, 2014 at 12:27 Comment(0)
K
1

Since Laravel 5 (and as recently as Laravel 11) it's recommended to use the path() method on the request directly:

$request->img->path();
// The same as $request->file("img")->path();

In addition, to get the file's filename and size:

$filesize = $request->img->getSize();
$filename = $request->img->getFilename();
Komsa answered 23/9 at 22:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.