Get the full path of a File using the Storage facade on Laravel

 Reading time ~2 minutes

Heads up: this article is over a year old. Some information might be out of date, as I don't always update older articles.

Introduction

This post is just a reference for the various ways of retrieving the full path of a file using the Storage facade in Laravel.

Under the hood the Storage facade it uses the Flysystem PHP Package by Frank de Jonge, allowing the developer to use the same API with different filesystem drivers (Local, SFTP, Amazon S3, Rackspace and so on).

In this post I’m interested in getting the full path of a file. I will use the local driver, whose methods are relative to the root directory defined in the configuration file config/filesystem.php. By default, this value is set to the storage/app directory.

Save a file

To store a file locally we can use this code:

<?php

Storage::disk('local')->put('file.txt', 'Contents');

Retrieve the file path

The Storage facade is just a reference to the FilesystemManager class. When we use the disk method we are actually returning an instance of FilesystemAdapter which contains a reference to the driver, an instance of a League\Flysystem\Filesystem class. This is the class that abstracts the comunication to the filesystem, as it passes the API calls to the specified adapter. For example, this is what we get if we dump the content of Storage::disk('local'):

FilesystemAdapter {#222 ▼
  #driver: Filesystem {#220 ▼
    #adapter: Local {#219 ▼
      #pathSeparator: "/"
      #permissionMap: array:2 [▶]
      #writeFlags: 2
      -linkHandling: 2
      #pathPrefix: "/.../apache2/htdocs/laravelapp/storage/app/"
    }
    #plugins: []
    #config: Config {#221 ▶}
  }
}

Notice that we are interested in getting the value of the pathPrefix variable, which contains the full path of where our file is stored. As you can see we first need to get the driver and then the adapter before getting the path prefix, so our first option is the following

  • Laravel 5.0+
<?php

$storagePath  = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();

You can also omit getDriver() because, thanks to PHP’s __call magic method the method call is forwarded directly to the driver.

<?php

$storagePath = Storage::disk('local')->getAdapter()->getPathPrefix();

The getPathPrefix() call is actually performed on the Flysystem Adapter class.

However if we want to obtain the full path of the file without appending on our own, we can use the following syntax:

<?php

$path = Storage::disk('local')->getAdapter()->applyPathPrefix($filename);
  • Laravel 5.4+

Since Laravel 5.4 we can use a shorter syntax to get the storage path:

<?php

$storagePath = Storage::disk('local')->path();

We can also pass the name of our file to get the full path:

<?php

$path = Storage::disk('local')->path($filename);

And here’s our result:

/.../apache2/htdocs/laravelapp/storage/app/file.txt

comments powered by Disqus

Nullable objects using JavaScript proxies

Introduction

JavaScript proxies are an ES6 (aka ES2015) feature that lets you wrap an existing object and intercept any …