<p>You need to set the scenario <code>SCENARIO_REPLACE</code> or <code>SCENARIO_CREATE</code> depending what you like to do</p>

<pre class="lang-PHP prettyprint-override"><code>$asset = new \craft\elements\Asset();
$asset-&gt;tempFilePath = $tmpFilePath;

$asset-&gt;setScenario(\craft\elements\Asset::SCENARIO_REPLACE);
</code></pre>

CC BY-SA 4.0
2018-09-17T06:21:19.890Z
2018-09-17T06:21:19.890Z
Robin Schambach
<p>In the CP, when we upload an image with a same name, craft will ask if we wanna keep both or replace.
So in plugin, is there any way to do the replace? </p>

<plugin-development><assets><image><upload>
Is there any way to replace the asset programatically?
# Is there any way to replace the asset programatically?

(Asked by: username_0 on 2018-09-17)

In the CP, when we upload an image with a same name, craft will ask if we wanna keep both or replace.
So in plugin, is there any way to do the replace?

**Tags:** plugin-development, assets, image, upload


---

## 1 answers

### Answer by username_1 on 2018-09-17. Score: 2

You need to set the scenario SCENARIO_REPLACE or SCENARIO_CREATE depending what you like to do
$asset = new \craft\elements\Asset();
$asset->tempFilePath = $tmpFilePath;

$asset->setScenario(\craft\elements\Asset::SCENARIO_REPLACE);


---

