M
any times you need to interact with files in your modules or coding.
In this article we will mention the most important functions that Drupal provides out of the box.
Private and Public paths
Get public uri:
use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\Core\StreamWrapper\PublicStream;
PublicStream::basePath();
Get the real path of public uri:
\Drupal::service('file_system')->realpath(PublicStream::basePath()):
Get private uri:
PrivateStream::basePath();
And real path for private uri:
\Drupal::service('file_system')->realpath(PrivateStream::basePath());
You would need to set private file path in settings.php as :
$settings['file_private_path'] = 'sites/default/private';
And this directory must exists and writable by web server (www-data).