<p>So, you can't run php directly like that in CraftCMS, they disabled that for security reasons. </p>

<p>Instead, you will need to create a plugin to run your php code. </p>

<p>If you need the result returned in your template you can setup a twig variable then call it in your template, otherwise you can use other methods to run your code: </p>

<p>You can start here: <a href="https://craftcms.com/docs/plugins/introduction" rel="nofollow noreferrer">https://craftcms.com/docs/plugins/introduction</a></p>

CC BY-SA 3.0
2017-01-24T10:11:52.617Z
2017-01-24T10:11:52.617Z
joep
<p>You can add some business logic to your site using the <a href="https://github.com/lindseydiloreto/craft-businesslogic" rel="nofollow noreferrer">Businesss Logic plugin.</a> It's pretty straightforward.</p>

CC BY-SA 3.0
2017-01-24T15:47:49.580Z
2017-01-24T15:47:49.580Z
Jan_dh
<p>If you're simply looking to execute some custom scripts that are completely standalone (i.e. they don't interact with the Craft application in any way), those files should live somewhere below your public web root (i.e. the <code>public</code> folder, or wherever your <code>index.php</code> file resides). It isn't possible to have Craft route PHP files from the <code>/templates</code> directory. </p>

<p>However, the problem with reading/executing arbitrary PHP files from the public root, is that Craft basically "hijacks" all requests to anything below that folder, and routes those requests to <code>index.php</code>. In order to read or execute an arbitrary PHP file below the public root, you'll need to override Craft's URL routing, for example by using <code>RewriteRule</code> directives in your <code>.htacesss</code> file if you're using Apache (also see <a href="https://craftcms.stackexchange.com/questions/14561/installing-other-php-app-in-sub-directory">this related thread</a>).</p>

<p>On the contrary, if your custom PHP scripts do need to interact with Craft in any way, you'll need to write a custom plugin.</p>

<p>Fortunately, Craft plugins are relatively simple to build (as long as you're familiar with PHP, of course). You can use the excellent <a href="https://pluginfactory.io" rel="nofollow noreferrer">Plugin Factory</a> to quickly scaffold a bare-bones plugin with a thoroughly commented codebase or use <a href="https://github.com/lindseydiloreto/craft-businesslogic" rel="nofollow noreferrer">the Business Logic plugin</a> as a starting point. Downloading <a href="https://straightupcraft.com/craft-plugins" rel="nofollow noreferrer">other available plugins</a> and looking at their source code is probably not a bad idea, either.</p>

<p>In any case, before digging in you should definitely skim through <a href="https://craftcms.com/docs/plugins/introduction" rel="nofollow noreferrer">the official docs on plugin development</a>. By far, the most important thing to know about Craft plugins is how they are structured and what the various moving parts do. That knowledge will also give you a nice overview of what a plugin's capabilities are, and how you'd go about implementing a certain feature in your own plugin.</p>

<p>One thing that the official docs doesn't really mention, which might be relevant to your use case, is how you make use of third party scripts or libraries. There are <a href="https://craftcms.stackexchange.com/a/4891/1098">lots of ways to skin that particular cat</a>, but the headline is that using <a href="https://craftcms.stackexchange.com/a/3021/1098">using Composer</a> if you can is the best option, though a simple <code>require()</code> call in your plugin's main class' <code>init</code> method will of course also work.</p>

<p>Also, keep in mind that when you're using third party scripts inside a Craft plugin, you will need to <a href="https://craftcms.stackexchange.com/a/11829/1098">deal with namespacing</a>.</p>

<p><strong>Edit</strong></p>

<p>The upcoming Craft 3 release adds another option for running PHP business logic – <a href="https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module" rel="nofollow noreferrer">Yii2 Modules</a>.</p>

CC BY-SA 3.0
2017-01-24T19:43:10.760Z
2018-01-05T16:15:25.627Z
2018-01-05T16:15:25.627Z
Mats Mikkel Rummelhoff
<p>My Craft CMS installation has <code>public</code> and <code>craft</code> in the root of the directory, with the domain pointing to the public folder. Where could I put custom PHP scripts that I can run in the browser? </p>

<p>I've tried it in the craft/templates folder (/craft/templates/m53ford). In this folder I can see the <code>HTML</code> or <code>XML</code> files by visiting <a href="http://feeds.rhinodigital.co.uk/m53ford/index.xml" rel="nofollow noreferrer">http://feeds.rhinodigital.co.uk/m53ford/index.xml</a> for example, but a PHP script doesn't run in the same place.</p>

<p>I've also tried in folders at the same root level (alongside the public and craft folders), and also within the public folder itself. Where in the directory tree could I place a PHP file that I can visit in the browser?</p>

<php><craft-features>
Where can I run custom PHP scripts?
# Where can I run custom PHP scripts?

(Asked by: username_0 on 2017-01-24)

My Craft CMS installation has `public` and `craft` in the root of the directory, with the domain pointing to the public folder. Where could I put custom PHP scripts that I can run in the browser?

I've tried it in the craft/templates folder (/craft/templates/m53ford). In this folder I can see the `HTML` or `XML` files by visiting <http://feeds.rhinodigital.co.uk/m53ford/index.xml> for example, but a PHP script doesn't run in the same place.

I've also tried in folders at the same root level (alongside the public and craft folders), and also within the public folder itself. Where in the directory tree could I place a PHP file that I can visit in the browser?

**Tags:** php, craft-features


---

## 3 answers

### Answer by username_3 on 2017-01-24. Score: 6 | Accepted answer

If you're simply looking to execute some custom scripts that are completely standalone (i.e. they don't interact with the Craft application in any way), those files should live somewhere below your public web root (i.e. the `public` folder, or wherever your `index.php` file resides). It isn't possible to have Craft route PHP files from the `/templates` directory.

However, the problem with reading/executing arbitrary PHP files from the public root, is that Craft basically "hijacks" all requests to anything below that folder, and routes those requests to `index.php`. In order to read or execute an arbitrary PHP file below the public root, you'll need to override Craft's URL routing, for example by using `RewriteRule` directives in your `.htacesss` file if you're using Apache (also see [this related thread](https://craftcms.stackexchange.com/questions/14561/installing-other-php-app-in-sub-directory)).

On the contrary, if your custom PHP scripts do need to interact with Craft in any way, you'll need to write a custom plugin.

Fortunately, Craft plugins are relatively simple to build (as long as you're familiar with PHP, of course). You can use the excellent [Plugin Factory](https://pluginfactory.io) to quickly scaffold a bare-bones plugin with a thoroughly commented codebase or use [the Business Logic plugin](https://github.com/lindseydiloreto/craft-businesslogic) as a starting point. Downloading [other available plugins](https://straightupcraft.com/craft-plugins) and looking at their source code is probably not a bad idea, either.

In any case, before digging in you should definitely skim through [the official docs on plugin development](https://craftcms.com/docs/plugins/introduction). By far, the most important thing to know about Craft plugins is how they are structured and what the various moving parts do. That knowledge will also give you a nice overview of what a plugin's capabilities are, and how you'd go about implementing a certain feature in your own plugin.

One thing that the official docs doesn't really mention, which might be relevant to your use case, is how you make use of third party scripts or libraries. There are [lots of ways to skin that particular cat](https://craftcms.stackexchange.com/a/4891/1098), but the headline is that using [using Composer](https://craftcms.stackexchange.com/a/3021/1098) if you can is the best option, though a simple `require()` call in your plugin's main class' `init` method will of course also work.

Also, keep in mind that when you're using third party scripts inside a Craft plugin, you will need to [deal with namespacing](https://craftcms.stackexchange.com/a/11829/1098).

**Edit**

The upcoming Craft 3 release adds another option for running PHP business logic – [Yii2 Modules](https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module).


---

### Answer by username_1 on 2017-01-24. Score: 2

So, you can't run php directly like that in CraftCMS, they disabled that for security reasons.

Instead, you will need to create a plugin to run your php code.

If you need the result returned in your template you can setup a twig variable then call it in your template, otherwise you can use other methods to run your code:

You can start here: <https://craftcms.com/docs/plugins/introduction>


---

### Answer by username_2 on 2017-01-24. Score: 2

You can add some business logic to your site using the [Businesss Logic plugin.](https://github.com/lindseydiloreto/craft-businesslogic) It's pretty straightforward.


---

