1

I'm having very confusing problems with a basic php include.

I'm currently working on a version 2 of a website, so my index.php is located in

www.mywebsite.co.uk/v2/index.php

I have a database config file located inside

www.mywebsite.co.uk/v2/assets/database.php

I have a form action located inside

www.mywebsite.co.uk/v2/assets/actions/action.php

I need to include my database.php file inside my action.php file, but I cant seem to find the right include path.

I've tried

include "database.php";
include "assets/database.php";
include "../assets/database.php";
include "../../assets/database.php";
include "../database.php";
include "../../database.php";
include "../../../database.php";
include "../../../assets/database.php";

I've even started using

include(dirname(__FILE__)."../database.php");

But no matter what i try, the include file cannot be found, and my action page returns a 500 server error.

What am I doing wrong?

UPDATE

I'm having the weirdest issue, and I really cant figure out whats going on.

I am including all my files normally, and they all work great, but as soon as it comes to a form action file, include just refuses to work.

I'm including a database OOP file in my header across the site, and it works great. I then have a form which submits using POST to an action file, and as soon as I include the database in my action file, it refuses to work. It will only work if I use

dirname(__FILE__)."my/path"

The location of the action file is in the same folder as all my other files which are using the include function, so the path is definitely correct, but whenever i try to include on my action file I get a 500 server error unless i use the dirname(FILE)."my/path" method.

Any ideas?

UPDATE 2

For some reason I have to use a different include path for 2 different files both in the same location.

For example database.php, home-slider.php and includes.php are all in the same directory. If I want database.php to access the includes file I can use

include "assets/includes.php";

But if i want to access the includes file using the home-slider.php page I have to use

include "../assets/includes.php";

Even though they are all in the same directory?

  • Possible duplicate of [PHP include relative path](http://stackoverflow.com/questions/17407664/php-include-relative-path) – Matt S May 19 '17 at 13:22
  • Using `$_SERVER['DOCUMENT_ROOT'].'/path/to/file'` works a lot better than trying to figure out relative paths. – aynber May 19 '17 at 13:23
  • I would recomond you that you use this:`require_once('../../database.php');` – Doggo May 19 '17 at 13:24
  • That one did work. Can I ask, why do I have to use the dirname() function? I'm aware of what the function does, but I thought include used the current file location as a reference, so just include "../../database.php"; but instead I have to use include(dirname(__FILE__)."../../database.php"); –  May 19 '17 at 13:26
  • have you modified your `.htaccess` file or Apache's virtual host configs at all? im wondering if you are somehow redirecting. – CodeGodie May 19 '17 at 13:26
  • @CodeGodie never use `include`, always use `require_once`, have a look at the official documentation – matiaslauriti May 19 '17 at 13:29
  • @matiaslauriti Can you explain why in your own words? – CodeGodie May 19 '17 at 13:43
  • @CodeGodie I tagged you and not @Sam, sorry !! Don't use `include` (if you are not returning anything from the file) and use `require_once` because `include` will TRY TO include the file, if it is not found, you will not get an exception and code will continue execution until an exception is thrown because that file functionality was not included. If you use `require_once` it will throw an exception if not found AND will only require it once if it was already required. – matiaslauriti May 19 '17 at 13:48
  • @matiaslauriti no problem. Though I would NOT say _"never use include"_, there are occasions where you would want to use it. Its really up to the dev and the application wether you want to require it or not. – CodeGodie May 19 '17 at 13:55
  • @CodeGodie yes, I am really used to use OOP, so I use `namespace`s and just `require_once` the class by autoloading. – matiaslauriti May 19 '17 at 14:00
  • @matiaslauriti yes i agree with that, namespaces are the way to go – CodeGodie May 19 '17 at 14:03

1 Answers1

0

If this is your file structure:

v2/
|
|-- assets/
|      |
|      |-- actions/
|      |      |
|      |      |-- action.php
|      |
|      |-- database.php
|
|-- index.php

Then by adding include '../database.php' on action.php should work. if it is not, then you might have something wrong your PHP file. You may also use require '../database.php'; to see what errors that gives you as it will throw errors regarding database.php.

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
  • i'm getting 2 errors on my page Warning: include(assets/includes.php): failed to open stream: No such file or directory in mydirectory\v3\assets\home-slider.php on line 3 and Warning: include(): Failed opening 'assets/includes.php' for inclusion (include_path='.;.\includes;.\pear') in mydirectory\v3\assets\home-slider.php on line 3 –  May 19 '17 at 14:38
  • the file does exist in that directory just to clarify, so I don't know why im getting that error. –  May 19 '17 at 14:39
  • is the file structure correct? I updated my answer.. doesnt make sense why you keep getting that error. – CodeGodie May 19 '17 at 14:51
  • Also, the error points to `assets/includes.php` thats a different file than the ones you mentioned. – CodeGodie May 19 '17 at 14:52
  • That is my file structure, but I have to use `../../database.php` in my action file. Almost like I have to navigate outside of the assets folder before I can navigate back into it. –  May 19 '17 at 15:05