Reputation: 1580
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.
Upvotes: 6
Views: 27239
Reputation: 6092
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();
Upvotes: 1
Reputation: 152890
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
Upvotes: 12