Mas Bagol
Mas Bagol

Reputation: 4617

How forward slash "/" actually work in php?

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.

Upvotes: 2

Views: 2929

Answers (2)

Mas Bagol
Mas Bagol

Reputation: 4617

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.

Upvotes: 3

Jigar
Jigar

Reputation: 3322

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');?>

Upvotes: 0

Related Questions