简体   繁体   中英

hard links between php files in different directories, what should be the expected behaviour of __FILE__

I have two websites that are identical in structure. The index.php file needs to generate different content depending on the domain that the page is being served from. I am using apache 2.2 and created two virtual hosts using two different folders under /var/www (/var/www/site.domain.com and /var/www/site.domain.ca)

I am using FILE to obtain the full path of the executing index.php and depending on the path I output the correct content.

Since the files are all the same, I wanted to use links to make editing the file easier, so instead of editing one of the index.php, then copying it to the other directory, I wanted editing either of files to update the other.

I used the cp -al command to copy with hard links:

cp -al /var/www/site.domain.com /var/www/site.domain.ca

The problem is that when I access the index.php file in one site vs. the other, the FILE variable does not reflect the path of which index.php file is executing. Depending on which domain I visit first, the FILE will reflect the path of that domain.

I will try using getcwd to see if that works, but can someone explain why this is happening. Shouldn't FILE reflect the current script that's executing.

These are hard links, not soft links.

Is apache caching the script, is APC the source of the problem?

Update: getcwd() worked, it seems to always return the correct current directory.

That's how hardlinks work. The system is not going to scan through the entire filesystem to try and figure out alternative names for the file. A hardlink is exactly that.. HARD. "this is an official, unchanging, immutable name for this file".

eg:

$ cat z.php
<?php
echo __FILE__, "\n"
$ mkdir subdir
$ cd subdir
$ ln ../z.php hardlink.php
$ ln -s ../z.php softlink.php
$ php hardlink.php
/home/marc/test/subdir/hardlink.php
$ php softlink.php
/home/marc/test/z.php

Note now the hardlink displays the location of the hardlink itself, while the softlink (aka symlink) displays the target of the link, not the link itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM