I would like to use a method of fileExists. It belongs to VolumeInterface. https://docs.craftcms.com/api/v3/craft-base-volumeinterface.html

I suppose the code would be like \craft\base\Volume::fileExists($path).

However, Craft returns an error;

Non-static method craft\base\VolumeInterface::fileExists() cannot be called statically

Could I ask how I should modify the code?


The normal way would be to instantiate and call non-statically like this:

(new \craft\base\Volume)->fileExists($path);

...However, that won't work here because it's an abstract class, which, by definition, cannot be instantiated. Assuming you're dealing with an Asset element(?), try calling getVolume() and then you should be able to call fileExists() from there. E.g:


| improve this answer | |
  • Yes, I would like to get asset's existence. However, it is from its URL, not $asset. For example, I want to get if the file exist; "assets/doc/RegionA/statement.pdf" – Kota.M Apr 22 at 18:14

I found an alternative way to find out if the asset exists or not, with filename, asset query and folder id. It is like;

{% set asset_query = craft.assets().filename(filename).folderId(folderID).all() %}

And if the query's length is 1 or more, it means the asset file exists.

| 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.