April 8, 2012

PHP include doesn't work

Question by Chris

I’m not sure how simple is this to solve, but I assume I’m doing something wrong. I’m new to PHP, so bear with me, please.

When I started learning PHP, I always placed all my project files into the same folder along with index.php and thus included everything like this:

<?php
include('./translation.php');
?>

Later on in the process of learning as I gained experience and my skill increased, I had to start using folders and place my files into sub folders. I ended up successfully including my files with the following:

<?php
include('../translation.php');
?>

My trouble-free coding took an unexpected turn when I decided to start using sub-sub folders. After placing all the files even deeper into the file structure I was shocked to find out that I cannot include them anymore, using:

<?php
include('.../translation.php');
?>

Now I’m lost. What did I do wrong? Am I to understand that I cannot include files deeper than 2 directories in the project? Should I start using a different file system?

Answer by Starx

You got the concept incorrect.

  • . represents current directory
  • .. represents parent directory

These are the way a file system keeps in track of the file and browse depth.

But you cannot use ... to change the directory. As you go deeper, you have to use

For example:

include('../../translation.php');

includes the files, two level outside of current directory. Which is probably what you were trying to do.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!