4

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?

3
  • In most filesystems, .. refers to the parent directory of the current one, so you can chain together as many as you need via ../../../../ etc, with a / between to delineate directories. The single dot . refers to the current directory, so ./././././ still refers to the same (current) directory. Commented Apr 8, 2012 at 17:26
  • As you create larger projects it becomes helpful to define a value like define('SITE_ROOT', dirname(__FILE__)); at the top level, and then include others as with include(SITE_ROOT . "/../translation.php"); Commented Apr 8, 2012 at 17:30
  • I already anshered some trouble at this post, maybe can help: stackoverflow.com/questions/3510909/… Commented Jul 30, 2014 at 20:52

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 but i would change "previous" to "parent". cause ".." is not the same a pressing the "back" button.
4

The include should be:

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

Each level 'up' needs its own ../.

For example, if you had the following directories:

a
   a1
      filex.php
   a2
b
   b1
      filey.php

If in filex.php, you wanted to include filey.php, you would do the following:

<?php
include('../../b/b1/filey.php');
?>

Up/out twice, down/ib twice

Basically, any time you want to go 'down' a level / into a folder, you give the name of the folder followed by a forward slash. But to go 'up' or out of the current folder (to the parent folder of the current folder) you give .. followed by the forward slash

Comments

1

If you want to go deeper (subfolder of subfolder), just use ../../

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

However, if your project is middle size or big size, I'd rather suggest you to define absolute path. In your index.php or whatever your included file is, define something like SITE_ROOT or INCLUDES_ROOT.

define("SITE_ROOT", dirname(__FILE__));

and then just use it on demand.

include(SITE_ROOT."/../../translation.php");

which is pretty useful in case you are including files from different folders

That said, imagine you have this folder structure

  • ROOT
    • includes/
      • boot.php
    • translations
      • en.php
    • ajax/
      • ajax2/
        • ajaxfile.php
    • index.php

CASE1: (fails)

index.php includes

<?php
include("includes/boot.php");

ajax/ajax2/ajaxfile.php includes

<?php
include("../../includes/boot.php");

includes/boot.php includes

<?php 
include("translations/en.php"); // from index.php it works but from ajax/ajax2/ajaxfile.php it searches for wrong file

CASE2: (passes)

ajax/ajax2/ajaxfile.php and index.php has the same content as the CASE1

includes/boot.php includes

<?php
define("INCLUDES_ROOT", dirname(__FILE__));
include(INCLUDES_ROOT."/../translations/en.php");

which passes, because INCLUDES_ROOT makes sure that the whole include point to the correct directory!

Comments

-1

Let's assume this tree of folders.

-> site
      -> public_html (Inside the site folder)
            ->  index.php (Inside the public_html folder)
      -> translation.php (Inside the site folder)

in your index.php you just have to use include "../translation.php";

if the translation.php is in the same folder as the site folder, you use include "../../translation.php";

if your translation.php is in the same folder as the index.php though, you just have to include like this: include "translation.php";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.