Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:59:30+00:00 2026-06-17T20:59:30+00:00

Short version: How do relative paths work in php? Long version: I have a

  • 0

Short version:
How do relative paths work in php?

Long version:
I have a local web site (http://test.com) and E:\test\ is its document root. This is my Directory tree:

  1. E:\test\001.php
  2. E:\test\exit.php
  3. E:\test\death.txt
  4. E:\test\mysql\first.php
  5. E:\test\mysql\death.txt
  6. E:\test\mysql\exit.php

And Contents of files:

E:\test\001.php

<?php

include('mysql/first.php');

?>

E:\test\exit.php

<?php
die('What?');
?>

E:\test\death.txt

Bonjour

E:\test\mysql\first.php

<?php
echo file_get_contents('death.txt');
include('exit.php');
?>

E:\test\mysql\death.txt

Hello there

E:\test\mysql\exit.php

<?php
die('DONE');
?>

If I browse http://test.com/001.php and E:\test\exit.php and E:\test\death.txt exist, I get this:

BonjourWhat?

And If they don’t exist:

Warning: file_get_contents(death.txt) [function.file-get-contents]: failed to open stream: No such file or directory in E:\test\mysql\first.php on line 2
DONE

How do relative paths workin php? I thought it should give me warning for both files or show proper output !

I’m testing this code on Win7 x64 with XAMPP, php 5.3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:59:32+00:00Added an answer on June 17, 2026 at 8:59 pm

    This is how the documentation on php.net describes the PHP include search mechanism:

    1. First, it will look in the include_path:
      http://php.net/manual/en/ini.core.php#ini.include-path.
    2. Then it will try to load the file in the calling script’s own directory if it could not find anything in the include_path. (in our
      case, the root directory)
    3. Finally, if everything else failed, it will then try to find the file in the current directory.

    When I ran your scripts, my include path was set to .: by default (the current directory and nothing else), so when I try to load death.txt and exit.php from mysql/first.php it should search in ./ folder which is the current directory. But… It does not. The current directory should be mysql/ but it’s not pointing there… So what’s happening here?

    PHP is quite special with that… Let’s rollback to the first statement we made. What is PHP interpreting in our include path when it uses the . is actually a path resolution again! So, looking at our path, we have . which can’t be interpreted (./. again?) to anything using our include_path. Then, PHP has to go to the second path resolution (2) which is the root directory of our script. (have you got an headache yet? Because I do)

    Using get_include_path() and realpath() you can observe this:

    Included path: '.:'
    Including mysql/first.php: /Users/Kouno/Development/SandBox/test-php/mysql/first.php
    This is what `realpath` returns for './': /Users/Kouno/Development/SandBox/test-php
    Getting content of death.txt: /Users/Kouno/Development/SandBox/test-php/death.txt
    Bonjour
    Including exit.php: /Users/Kouno/Development/SandBox/test-php/exit.php
    What?
    

    Then, why does it specifically fail to load mysql/death.txt but still find mysql/exit.php, once you remove death.txt and exit.php from your root directory? It should find both of them, right?

    Well, it’s because get_file_content has a second parameter which ignores include_path by default.

    If you set the second parameter:

    echo file_get_contents('death.txt', FILE_USE_INCLUDE_PATH);
    

    Then you would get the expected result.

    Included path: '.:'
    Including mysql/first.php: /Users/Kouno/Development/SandBox/test-php/mysql/first.php
    Getting content of death.txt: (no path returned)
    Hello there
    Including exit.php: (no path returned)
    DONE
    

    The funny thing is that get_file_content() will start using the same mechanism as include (and this is not documented properly on php.net). And realpath() doesn’t even implement this mechanism at all (which explains why I have a no path returned showing up).

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version: I need mysql code that will change [href=http://a-random-domain.com]hyperlink[/href] into <a href=http://a-random-domain.com>hyperlink</a> without
Short version: Can I grant access to external databases to a role? Long version:
Short version : echo testing | vim - | grep good This doesn't work
Short version: I have a similar setup to StackOverflow. Users get Achievements. I have
Short version: I have an MSBuild project that imports another project. There is a
I have two files with the paths: /reader.php /ebook/titlepage.xhtml I plan for the reader
Working on this page: http://www.karlsenner.dreamhosters.com/about.php and having trouble with the navigation in IE6. It
Short version: I want a way to run somefunction(username) and have it return the
Short version of what I want to accomplish : I have a foot pedal
Short Version For those who don't have the time to read my reasoning for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.