andre0ani's avatar

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

0 likes
7 replies
bobbybouwmann's avatar

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 ;)

1 like
munazzil's avatar

@andre0ani

Check like below.

@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 
andre0ani's avatar

Effectivelly, dd(files) shows nothing... There's an error in my code to get the data about the files.

andre0ani's avatar

Ok, I can list files in my uploads directory ;-) But now, I woul'd like to have a link to the file and show the size of them.

The link is not good, I have : http://localhost/storage/files

I'm using this : $url = Storage::url('files');

And for the size, I don't know how to use this, in the doc : $size = Storage::size('file.jpg'); If I put $files, it's not working...

andre0ani's avatar

I can get some informations about file upload but not size or mimetype, I have this error : File not found at path: fileName1541607795.png

$ext = \File::extension($fileName);
      $path = Storage::disk('public')->path($fileName);
      
      $size = Storage::disk('public')->size($fileName);
      $type = Storage::disk('public')->mimeType($fileName);


      return back()
          ->with('success','Fichier envoyé.');

And I don't know how to pass these variables with the return back and the success message.

andre0ani's avatar

I'm lost... I don't understand how files working. I can't get the path to download the file, or it's size, after upload it. This is the function to show the view :

  public function uploadFile(Request $request){

    $files = Storage::files('uploads');
    $url = Storage::url('files');

    $path2 = Storage::files('uploads')->url($files->path);

    $path = Storage::disk('public')->path('files');

    return view('uploadfile', [
        'files' => $files,
        'url' => $url,
        'path' => $path,
        'size' => $size
      ]);
    }

A little help please ? Thanks,

andre0ani's avatar

Always trying to understand files in Laravel... Get informations about upload files (path for uplaod, size, etc).

$files_with_size = array();
    $files = Storage::files('uploads');
    foreach ($files as $key => $file) {
      $files_with_size[$key]['name'] = $file;
      $files_with_size[$key]['size'] = Storage::disk('uploads')->size($file);
      
    }

I've got this error : Driver [] is not supported.

Any help please, again ?

Please or to participate in this conversation.