4

I got the following error when a file is included.

 Warning:  require(home2/myusername/public_html/inc/config.php) 
 [function.require]: failed to open stream: No such file or directory in 
 /home2/myusername/public_html/inc/bootstrap.php on line 32

 Fatal error:  require() [function.require]: Failed opening required 
 'home2/myusername/public_html/inc/config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in 
 /home2/myusername/public_html/inc/bootstrap.php on line 32

It is working fine in my localhost running on Windows PC. But I got that error when I uploaded the files on my shared hosting server which is CentOS.

home2/myusername/public_html/index.php includes /inc/bootstrap.php which is then trying to include /inc/config.php. The file config.php does exist on the server. home2/myusername/public_html/ is returned by the function getcwd().

The below is the first few lines of codes in bootstrap.php which issues the error.

define('APP_DIR', 'app');   

if( !defined('APP_ROOT') ){
    $APP_ROOT = trim(getcwd(), '/').'/';
    if(isset($_GET['bootstrap'])) $APP_ROOT .= APP_DIR . '/';
    define('APP_ROOT', $APP_ROOT);
}

if( !defined('ROOT') ){
    $ROOT = str_replace(APP_DIR, '', trim(APP_ROOT, '/'));
    if( strrpos($ROOT, '/') != strlen($ROOT)-1 ) $ROOT .= '/'; 
    define('ROOT', $ROOT);
}

# path to inc/ folder
define('INC', ROOT.'inc/');

# System configuration variables
require INC . 'config.php';

I have also a URL Rewrite in /public_html/.htaccess.

RewriteRule ^index.php$ index.php?bootstrap [L]

So, when I browse example.com/index.php, it rewrites to example.com/index.php?bootstrap. It is the situation I got the error.

Here is my directory structure:

/public_html/
    |__ inc
    |   |__ bootstrap.php
    |   |__ config.php
    |__ .htaccess
    |__ index.php <--- I'm browsing this    

I think the problem would be related to the absolute path file include. The relative path file include require 'inc/bootstrap.php in index.php is okay.

Sithu
  • 4,752
  • 9
  • 64
  • 110
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Apr 30 '16 at 14:20

2 Answers2

5

If you file is located in:

/home2/myusername/public_html/index.php

and you bootstrap.php file to include is located in:

/home2/myusername/public_html/inc/bootstrap.php

your include line correctly is:

include_once $_SERVER['DOCUMENT_ROOT']."/inc/bootstrap.php";

$_SERVER['DOCUMENT_ROOT'] is equal to /home2/myusername/public_html

Joseph Collins
  • 441
  • 2
  • 9
1

Most likely your paths are not correctly configured. It's an easy solution most of the time. Can you please post the code of config.php and bootstrap.php where you include the files.

It's most likely that you have to change the following.

/inc/config.php to config.php.

Since it's in the same folder.

EDIT:

You are missing a '/'

Failed opening required 'home2/myusername/public_html/inc/config.php' 

Should be this

'/home2/myusername/public_html/inc/config.php'
Rimble
  • 873
  • 8
  • 22
  • I'm browsing `/public_html/index.php`. The `config.php` is located in `/public_html/inc/'. So, I have the correct paths. – Sithu Feb 22 '14 at 16:17
  • You are missing a / `Failed opening required 'home2/myusername/public_html/inc/config.php'` Should be this `'/home2/myusername/public_html/inc/config.php'` – Rimble Feb 22 '14 at 16:37
  • Yes, it is because of missing the left `/`. I replaced `trim()` with `rtrim()`. Thanks! – Sithu Mar 15 '14 at 03:27
  • Haha good thing you found it. Too bad my answer didn't really apply to your problem. But glad I could help. – Rimble Mar 15 '14 at 03:36
  • You could add an **EDIT** to your answer, but do not remove the original answer. Then, I could accept your answer for the sake of further visitors. – Sithu Mar 15 '14 at 07:37
  • Edited my answer with the content of the comments for future sake. – Rimble Mar 16 '14 at 07:16