0

I'm new on Laravel and some little trick escape me.

i have a controller where i need to list pictures in a public folder ( ex: public/images/album/Ecosse), but i don't known how to acceed it. i have always the no directory error. Can you explain me how it's working ?

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repositories\AlbumsRepository;
use File;

class AlbumController extends Controller
{
    protected $albumsRepository;

    public function __construct(AlbumsRepository $albumsRepository)

    {
        $this->albumsRepository = $albumsRepository;
    }
    public function show($n)
    {
        $album = $this->albumsRepository->getById($n);

        $files1 = File::allFiles(asset('images/albums/'.$album->titre));
        foreach ($files1 as $key => $value)
           {
              if (!in_array($value,array(".","..")))
              {
                 if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
                 {
                    $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
                 }
                 else
                 {
                    $result[] = $value;
                 }
              }
           } 
        return view('album')->with('album', $album);
    }
}

Thank for your help. Maxime

3 Answers 3

1

use RecursiveDirectoryIterator.

$directory = new RecursiveDirectoryIterator(public_path('images/albums/'.$album->titre));
$iterator = new RecursiveIteratorIterator($directory);
foreach ($iterator as $item) {
    // both file and directory will be iterated.
}
Sign up to request clarification or add additional context in comments.

2 Comments

sorry for late answer but i'm in local, maybe it's change something because i have this now for directory : C:\wamp64\www\photo\public\images/albums/Ecosse. Maybe i need to replace \ by / ?
@MaximeBaude No need to replace manually, windows can recognize both '\' and '/' directory delimiter.
1

I access my public folder from controller by using url() function like:

url('uploads/images/user_image.png');

Comments

0

This works

public_path('cow.jpg')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.