0

As I have learned so far, using the ./ reference to the current folder, while the ../ references one level up the current folder, and using / references the root directory. So when adding the relative path inside an image source's attribute in all three conditions it works fine, but when trying to use the three ways with Include, Both ./ and / references the current folder. Is there any misunderstanding? because I am confused. the code I use :

<?php

echo '<strong>This is the child using ./ </strong><br><br><img src="./image.jpg"><br><br>';
echo '<strong>This is the parent using ../ </strong><br><br><img src="../image.png"><br><br>';
echo '<strong>This is the root using / <br><br><img src="/image.png"></strong><br>';
include('./file.php');
include('../file.php');
include('/file.php');

?>

Johannes
  • 64,305
  • 18
  • 73
  • 130
Mohamed Omar
  • 453
  • 5
  • 16
  • This StackOverflow [answer](http://stackoverflow.com/a/7378824/371392) might give you some clarity on this topic. – Web User Nov 03 '16 at 22:05
  • You may have restrictions in place for your include path, so the root / is not actually the root. – Progrock Nov 03 '16 at 22:06
  • but i can reference the root if i am not using the include – Mohamed Omar Nov 03 '16 at 22:07
  • Example of your root referencing please. – Progrock Nov 03 '16 at 22:15
  • I am working in localhost, and i have added three images with the same name putting one in the root and one in a parent folder and The third in a child folder and this also applies for another three files with a name file.php . here is the code `


    '; echo 'This is the parent using ../



    '; echo 'This is the root using /


    '; include('./file.php'); include('../file.php'); include('/file.php'); ?>`
    – Mohamed Omar Nov 03 '16 at 22:20
  • Sorry, i don't know why it has been added with such format. you can copy/past to external file. Here is a link to localhost folder [link] (http://www.mediafire.com/file/8qilbl9sjd8leza/localhost.zip) – Mohamed Omar Nov 03 '16 at 22:22
  • It looks as if you are confusing the absolute file path root with the web server's document root. Does that last include `include '/file.php'` work? – Progrock Nov 03 '16 at 22:25
  • It worked but it referenced the current folder. and as you can see it referenced the localhost when used within the image source's attribute – Mohamed Omar Nov 03 '16 at 22:28
  • Try listing the output of `var_dump(scandir('/'));` – Progrock Nov 03 '16 at 22:36
  • Oh, when used the `var_dump(scandir('/'));` it referenced the the main partition that the server works on. I am using Xampp through the partition E – Mohamed Omar Nov 03 '16 at 22:42
  • If you remove the other includes, does `include '/file.php'`; do anything? Or better still `require '/file.php'`? Assuming `file.php` is NOT in your main server partition. – Progrock Nov 03 '16 at 22:47
  • Both give the same result – Mohamed Omar Nov 03 '16 at 22:49
  • Please list your XAMPP Version and any configuration/customisation. – Progrock Nov 04 '16 at 09:48
  • I am using a pure installation of Xampp 3.2.2 – Mohamed Omar Nov 04 '16 at 11:03

1 Answers1

0

You can tweak the Php environment, here is an example:

<?php    
chroot(__DIR__);
include '/tmp/foo.txt';

Rather than including the absolute path /tmp/foo.txt, from the system root /. The root is changed to the current directory and it references the folder tmp in there.

The example above will work as a CLI script run as root. But there may be other ways to chroot the Php environment.

Public references to assets that begin with a forward slash are relative to the document root. Suppose we have a file system like the following, where /foo is the document root.

/foo
|-- bar.png
|-- baz
|   |-- bat.html
|   `-- man.ogg
`-- home.html

Using absolute paths: if we want to reference man.ogg from home.html, we would use a path /baz/man.ogg, if we were to reference bar.png from bat.html, we would use the path /bar.png. I hope you can see that starting with a slash is not the same as referencing the current directory.

Progrock
  • 7,373
  • 1
  • 19
  • 25
  • Thanks for your answer, but my question is why the relative path works differently when added within the image source's attribute from being included. – Mohamed Omar Nov 03 '16 at 22:36
  • Absolute public paths are relative to the document root. – Progrock Nov 03 '16 at 22:40
  • Sorry i don't understand, would you please demonstrate with an example – Mohamed Omar Nov 03 '16 at 22:47
  • You have written: `with Include, Both ./ and / references the current folder.`, I want for you to double check that your statement holds true, because that is NOT the expected behaviour. – Progrock Nov 03 '16 at 22:51
  • yes it does . i have sent you the complete folder link . you can download it, use the local server and check the results – Mohamed Omar Nov 03 '16 at 22:52
  • Using your file structure. and running index.php in the deepest folder I get this output at the end: `This is the child folderThis is the parent folder`. What does yours output? – Progrock Nov 03 '16 at 23:32
  • On localhost you will get (This the child folder this is the parent folder this is the child folder) but on online server it results to (This the child folder this is the parent folder). which means it can't access the root on an online server my be due some restrictions but i think it can access it on the localserver – Mohamed Omar Nov 04 '16 at 07:46
  • Your localhost behaviour is odd, and not what I'd expect. Your online output, mimics mine. If you change the include statements to require, it should throw a fatal error along the lines of `/file.php` doesn't exist on the on-line server, and that should make sense, unless you have placed your php file at the system root. – Progrock Nov 04 '16 at 09:56