# Cannot include or require any php file in slim index.php

**URL:** <https://discourse.slimframework.com/t/cannot-include-or-require-any-php-file-in-slim-index-php/2479>\
**Category:** Questions\
**Created:** [June 16, 2018, 12:21pm UTC](https://discourse.slimframework.com/t/cannot-include-or-require-any-php-file-in-slim-index-php/2479 "2018-06-16T12:21:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjksd](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@jjksd](https://discourse.slimframework.com/u/jjksd)\
**Post date:** [June 16, 2018, 12:21pm UTC](https://discourse.slimframework.com/t/cannot-include-or-require-any-php-file-in-slim-index-php/2479/1 "2018-06-16T12:21:18Z")

</div>

So i’m trying to include my custom php file w/ my functions in index.php like this: require\_once(custom\_directory/custom\_file.php);  
and i’m getting bunch of errors (PHP Fatal error: require\_once(): Failed opening required ‘api/custom\_directory/custom\_file.php’ (include\_path=’.:/usr/share/php’) in /var/www/site/api/index.php on line 3)  
**BUT** if i’m including php file from the _same directory_ as index.php then everything is fine.  
Is it possible to fix this? Maybe it’s because of my .htaccess file?

My project structure:  
/var/www/site:  
– api  
---- vendor (slim is here)  
---- composer.json  
---- composer.lock  
---- .htaccess  
---- custom\_directory -\> custom\_file.php  
---- index.php (i’m here)

My .htaccess file:  
RewriteEngine On  
RewriteCond %{REQUEST\_FILENAMEf  
RewriteCond %{REQUEST\_FILENAMEd  
RewriteRule ^ index.php [QSA,L]

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [June 16, 2018, 12:43pm UTC](https://discourse.slimframework.com/t/cannot-include-or-require-any-php-file-in-slim-index-php/2479/2 "2018-06-16T12:43:09Z")

</div>

First, it looks as though your `vendor` folder is publicly accessible, which is a security risk.

I’d also note the error message doesn’t seem to match the `require` code you posted as the error includes an api directory, so this might not be exactly what you are after.

You might find it helpful to start your require from the current directory, so something like this in your case in the `index.php`.

```php
require __DIR__. '/custom_directory/custom_file.php';

```

---

<div class="post-metadata">

**Author:** ![jjksd](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@jjksd](https://discourse.slimframework.com/u/jjksd)\
**Post date:** [June 16, 2018, 1:11pm UTC](https://discourse.slimframework.com/t/cannot-include-or-require-any-php-file-in-slim-index-php/2479/3 "2018-06-16T13:11:41Z")

</div>

I’ve tried your suggestion and it’s helped me, thanks  
Also what i need to do w/ vendor folder to remove security risks?

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [June 16, 2018, 7:51pm UTC](https://discourse.slimframework.com/t/cannot-include-or-require-any-php-file-in-slim-index-php/2479/4 "2018-06-16T19:51:02Z")

</div>

The vendor folder shouldn’t be publicly accessible. In your structure, it looks like your web root is at `/var/www/site/api`, so your vendor folder should go outside that directory, perhaps as `/var/www/site/vendor`. Although if your web root is `var/www/site` and you are accessing urls like `http://example.com/api` then your vendor folder should be in `/var/www/vendor`.

In short, the only PHP file within your web root should typically be the front controller (`index.php` file).
