2

I confused about how forward slash work in php. Is it return the web root? Or computer files root? Or something else?

I tried several things, for example, my file structure in ubuntu server is like this:

/var/www/html/domainname.com/. --/config/. --/core.php --/images/. --/includes/. --/footer.php --/header.php --/navigation.php --/index.php 

This is work (all of them included in index.php):

<img src="/images/logo.png" /> # in header.php <a href="/">Home</a> # in navigation.php <a href="/test/test.php">test</a> # in index.php itself 

This doesn't work:

<?php require_once('/config/core.php'); ?> <?php include_once('/includes/header.php');?> <?php include_once('/includes/navigation.php');?> <?php include_once('/includes/footer.php');?> 

Why the last four examples doesn't work? The "/" character should returns domainname.com right? Or it returns "/var/www/html/" instead?

My goal is to put all files that needed to be included in includes directory. So, every .php file can access them relatively even if the .php file stored in sub directory.

4
  • its relative to the current running script, just add a .. Commented Feb 23, 2015 at 13:21
  • So, in index.php case, it wil return /var/www/html/ like that? Commented Feb 23, 2015 at 13:22
  • no, just check your include_path settings, if you start with / it will start looking on that include_path setting. in your case its ../ to move one level back from where your script is running Commented Feb 23, 2015 at 13:31
  • @BagolDaplun / is always /. And / is root folder. For UNIX OS its... /! For Windows, it's usually C:, but sometimes apache will return your access folder. To be sure, always use relative paths. Commented Feb 23, 2015 at 13:39

2 Answers 2

3

Now I understand. The forward slash / works differently in .php and .html documents.

in .php files, / means the root folder of computer, / in unix and C: in windows like Forien said in the comment. But, in .html files, / means the root folder of website or /var/www/html/domainname.com/ in my case. That's why I confused lmho.

So, this code:

<a href="/">Home</a> <img src="/images/logo.png" /> 

will look in /var/www/html/domainname.com/ even though it's written in .php files. Because .php files send it to browsers as .html files. The same goes for this one:

<?php echo '<a href="/">Home</a>'; ?> 

This one above still look in /var/www/html/domainname.com/ because .php files echoed it as .html files to web browsers.

But, this will be different:

<?php require_once '/includes'; ?> 

It will look in / in unix or C: in windows. Because .php files work in server computer. That's why the last one will not work. There's no /includes directory in server root directory. So, in order to make it works, I have to use this:

<?php require_once $_SERVER['DOCUMENT_ROOT'].'/includes'; ?> 

That's my conclusion if I'm correct.

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

Comments

0

Why the last four examples doesn't work? The "/" character should returns domainname.com right?

Wrong. It literally searches for the path /config/core.php, /includes/header.php, etc.

Remove / from the beginning of the path or add ./, then it will search from current working directory. You can use ./ and ../ for searching files relatively.

You can use/set PHP configuration include_path too.

http://php.net/manual/en/ini.core.php#ini.include-path

And also look logs for better understanding. :)

UPDATE: Also check chdir, this might be helpful to you.

chdir('/var/www/html/domainname.com'); <?php require_once('./config/core.php'); ?> <?php include_once('./includes/header.php');?> <?php include_once('./includes/navigation.php');?> <?php include_once('./includes/footer.php');?> 

1 Comment

php.net/manual/en/errorfunc.configuration.php#ini.error-log It is not set generally, so check apache logs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.