<h2>Update for Craft 3:</h2>

<p>To get the same output as Mike's path above, you can use:</p>

<pre><code>$volumePath = $asset-&gt;getVolume()-&gt;settings['path'];
$folderPath = $asset-&gt;getFolder()-&gt;path;
$assetFilePath = Yii::getAlias($volumePath) . $folderPath . $asset-&gt;filename;
</code></pre>

CC BY-SA 4.0
2018-06-20T15:34:42.227Z
This may only work for assets at the top-level of an asset volume. Apt to need an intermediate `$folderPath = $asset->getFolder()->path;`, like the Craft 2 answer.
August Miller
CC BY-SA 4.0
2018-06-20T14:10:47.120Z
2018-06-20T15:47:30.567Z
2018-06-20T15:47:30.567Z
jtenclay
<p>Here is an approach in Craft 4.
This approach assumes you have an asset element (<a href="https://docs.craftcms.com/api/v4/craft-elements-asset.html#public-properties" rel="noreferrer">craft\elements\Asset</a>).</p>
<pre><code>use Craft;
//$asset = craft\elements\Asset;
$fsPath = Craft::getAlias($asset-&gt;getVolume()-&gt;fs-&gt;path);
$attachment = $fsPath . DIRECTORY_SEPARATOR . $asset-&gt;getPath();
</code></pre>

CC BY-SA 4.0
2022-05-27T02:52:06.603Z
2022-05-27T02:52:06.603Z
a-am
<p>See <code>Asset::getImageTransformSourcePath()</code> for how Craft CMS resolves the real local path. (<a href="https://github.com/craftcms/cms/blob/develop/src/elements/Asset.php#L1200" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/develop/src/elements/Asset.php#L1200</a>)</p>

<p>Should look something like this:</p>

<pre><code>$volume = $this-&gt;getVolume();
$assetFilePath = $volume instanceof LocalVolumeInterface
  ? FileHelper::normalizePath($volume-&gt;getRootPath() . DIRECTORY_SEPARATOR . $this-&gt;getPath())
  : null;
</code></pre>

CC BY-SA 4.0
2020-03-02T15:15:15.693Z
2020-03-02T15:15:15.693Z
DerCapac
<p>You can get the rest of the path from your asset's source and folder, I think something like this should do it:</p>

<pre><code>$source = $asset-&gt;getSource();

if ($source-&gt;type != 'Local')
{
    throw new Exception(Craft::t('Paths not available for non-local asset sources'));
}

$sourcePath = $source-&gt;settings['path'];
$folderPath = $asset-&gt;getFolder()-&gt;path;

$assetFilePath = $sourcePath.$folderPath.$asset-&gt;filename;
</code></pre>

CC BY-SA 3.0
2018-03-15T11:37:44.750Z
Doesn't work for me :( it results in `{basePath}uploads/example.png` which isn't a valid path obviously
Stalinko
CC BY-SA 4.0
2020-10-14T14:35:52.170Z
@Stalinko You can use craft()->config->parseEnvironmentString() to replace `{basePath}` with the config basePath like this `$assetFilePath = craft()->config->parseEnvironmentString($sourcePath.$folderPath.$asset->filename);`
Koen Rijpstra
CC BY-SA 4.0
2020-10-15T03:01:22.067Z
thanks @K.Rijpstra but I haven't been working with craft cms since 2018 :))
Stalinko
CC BY-SA 3.0
2015-08-01T12:07:22.263Z
2015-08-01T16:54:31.740Z
2015-08-01T16:54:31.740Z
Mike Pepper
<p>I have some csv files uploaded as assets and now I need to <code>fopen()</code> them from my plugin.</p>

<p>I have an <code><a href="http://buildwithcraft.com/classreference/models/AssetFileModel">AssetFileModel</a></code> but I have not been able to work out how to construct a full path to the file.</p>

<p>I can access the filename like: <code>$asset->filename</code> which gives me <code>my_csv_file.csv</code> but this is not enough for <code>fopen()</code> to find it.</p>

<p>How can I get the absolute path to the asset file? </p>

<p>Or at least a path that I can use with <code>fopen()</code> to find the file?</p>

<plugin-development><assets><assetfilemodel>
How to get absolute path to asset file?
# How to get absolute path to asset file?

(Asked by: username_0 on 2015-07-31)

I have some csv files uploaded as assets and now I need to `fopen()` them from my plugin.

I have an `AssetFileModel` but I have not been able to work out how to construct a full path to the file.

I can access the filename like: `$asset->filename` which gives me `my_csv_file.csv` but this is not enough for `fopen()` to find it.

How can I get the absolute path to the asset file?

Or at least a path that I can use with `fopen()` to find the file?

**Tags:** plugin-development, assets, assetfilemodel


---

## 4 answers

### Answer by username_5 on 2015-08-01. Score: 15 | Accepted answer

You can get the rest of the path from your asset's source and folder, I think something like this should do it:
$source = $asset->getSource();

if ($source->type != 'Local')
{
    throw new Exception(Craft::t('Paths not available for non-local asset sources'));
}

$sourcePath = $source->settings['path'];
$folderPath = $asset->getFolder()->path;

$assetFilePath = $sourcePath.$folderPath.$asset->filename;

> **Comments:**
> **username_6**: Doesn't work for me :( it results in `{basePath}uploads/example.png` which isn't a valid path obviously  
> **username_7**: @username_6 You can use craft()->config->parseEnvironmentString() to replace `{basePath}` with the config basePath like this `$assetFilePath = craft()->config->parseEnvironmentString($sourcePath.$folderPath.$asset->filename);`  
> **username_6**: thanks @K.Rijpstra but I haven't been working with craft cms since 2018 :))  

---

### Answer by username_1 on 2018-06-20. Score: 12

Update for Craft 3:
To get the same output as Mike's path above, you can use:
$volumePath = $asset->getVolume()->settings['path'];
$folderPath = $asset->getFolder()->path;
$assetFilePath = Yii::getAlias($volumePath) . $folderPath . $asset->filename;

> **Comments:**
> **username_2**: This may only work for assets at the top-level of an asset volume. Apt to need an intermediate `$folderPath = $asset->getFolder()->path;`, like the Craft 2 answer.  

---

### Answer by username_3 on 2022-05-27. Score: 6

Here is an approach in Craft 4.
This approach assumes you have an asset element (craft\elements\Asset).
use Craft;
//$asset = craft\elements\Asset;
$fsPath = Craft::getAlias($asset->getVolume()->fs->path);
$attachment = $fsPath . DIRECTORY_SEPARATOR . $asset->getPath();


---

### Answer by username_4 on 2020-03-02. Score: 4

See Asset::getImageTransformSourcePath() for how Craft CMS resolves the real local path. (https://github.com/craftcms/cms/blob/develop/src/elements/Asset.php#L1200)
Should look something like this:
$volume = $this->getVolume();
$assetFilePath = $volume instanceof LocalVolumeInterface
  ? FileHelper::normalizePath($volume->getRootPath() . DIRECTORY_SEPARATOR . $this->getPath())
  : null;


---

