6

I'm trying to find the way to convert an uri like

public://field/image/link-carousel.png

to a relative path

sites/default/files/directory/link-carousel.png

(of course this is an example because public:// could have other path).

How to do it?

Code:

 if(isset($article_node['field_image']['und']['n0'])){  
      $uri = $article_node['field_image']['und']['n0']['uri'];
      $realpath = \Drupal::service('file_system')->realpath($uri);
      $path = str_replace($_SERVER['DOCUMENT_ROOT'] . '/', '', $realpath);   
}

here on printing $uri will get public://field/image/link-caribbean-carousel-epic-press-release.png.on printing $realpath it gives a blank page.

Moby M
  • 910
  • 2
  • 7
  • 26
  • This is a duplicate of drupal stack exchange, see answer here https://drupal.stackexchange.com/questions/193869/get-the-real-path-of-a-file – powpow12 Mar 05 '19 at 14:15

4 Answers4

14

Taken from https://gist.github.com/illepic/fa451e49c5c43b4a1742333f109dbfcd:

// public://images/blah.jpg
$drupal_file_uri = File::load($fid)->getFileUri();
// /sites/default/files/images/blah.jpg
$image_path = file_url_transform_relative(file_create_url($drupal_file_uri));
rpayanm
  • 6,303
  • 7
  • 26
  • 39
  • This can now be simplified to `$image_path = File::load($fid)->createFileUrl()` to get the relative file path or `$image_path = File::load($fid)->createFileUrl(FALSE)` to get the absolute file path. – leymannx May 29 '21 at 17:02
2
/** @var Drupal\file\Entity\File $file */
$file = // get file.
$file->createFileUrl(TRUE); // to get relative path
$file->createFileUrl(FALSE); // to get absolute path https://

Link : https://api.drupal.org/api/drupal/core%21modules%21file%21src%21Entity%21File.php/function/File%3A%3AcreateFileUrl/8.7.x

1

Since Drupal 9.3 you can do:

$relativePathToFile = \Drupal::service('file_url_generator')->generateString($thumbnailUri);

See: https://www.drupal.org/node/2940031

hugronaphor
  • 948
  • 8
  • 23
0

Use FileSystem::realpath() to convert from sream-wrapped URIs:

use Drupal\Core\File\FileSystem;

//$realpath = drupal_realpath($uri); // D7, D8 deprecated
$realpath = FileSystem::realpath($uri);
$path = str_replace($_SERVER['DOCUMENT_ROOT'].'/','',$realpath);

UPD: For some reason the above example is not valid, we need to use file_system service instead:

  $uri = $node->field_document->entity->getFileUri();
  $realpath = \Drupal::service('file_system')->realpath($uri);
  $path = str_replace($_SERVER['DOCUMENT_ROOT'] . '/', '', $realpath);
Dmytro Sukhovoy
  • 952
  • 5
  • 17
  • updated code also not working.i will update the code that iam working on – Moby M Dec 19 '17 at 09:56
  • @MobyM, the $uri = $node->field_document->entity->getFileUri(); string is here just to have a general idea on how to get uri form the node's file field (as most common use-case), I'm not aware of your filed names and variables to craft ready-to use snippet for you... – Dmytro Sukhovoy Dec 19 '17 at 10:36
  • It' s not about field names and variables.what ever the variable may be $uri must be like public://field/image/link-carousel.png.your mentioned code $realpath = \Drupal::service('file_system')->realpath($uri); $path = str_replace($_SERVER['DOCUMENT_ROOT'] . '/', '', $realpath); these lines should be correct with it. – Moby M Dec 19 '17 at 11:38
  • The string \Drupal::service('file_system')->realpath($uri) is totally fine, you can find it in core and contrib modules. Refer to https://api.drupal.org/api/drupal/core%21includes%21file.inc/function/drupal_realpath/8.2.x and https://api.drupal.org/api/drupal/core!lib!Drupal!Core!File!FileSystem.php/function/FileSystem%3A%3Arealpath/8.2.x for documentation. The only guess I have why you're getting white screen is unreadable/inexistent file at public://field/image/link-caribbean-carousel-epic-press-release.png – Dmytro Sukhovoy Dec 19 '17 at 17:20
  • Please provide more info on where and how you're using this snippet. – Dmytro Sukhovoy Dec 19 '17 at 17:27
  • please refer the url https://stackoverflow.com/questions/47749731/attaching-image-files-to-nodes-programmatically-in-drupal-8 – Moby M Dec 20 '17 at 06:04