15

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?

15

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;
| improve this answer | |
  • Doesn't work for me :( it results in {basePath}uploads/example.png which isn't a valid path obviously – Stalinko Mar 15 '18 at 11:37
8

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;
| improve this answer | |
  • 1
    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 Jun 20 '18 at 15:34
1

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;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.