1

I have the following test structure:

/www/index.php

<?php
require_once(dirname(__FILE__).'/linked/linked.php');

/www/linked/ which is a symlink to /symlinkedfolder/

/symlinkedfolder/linked.php

<?php
echo __FILE__;

The output for this script is:

/symlinkedfolder/linked.php

Is there any way/technique with PHP or Apache or Linux which would make symlink behave not symlink instead like a normal filesystem folder/file? I need that my example give back the following output:

/www/linked/linked.php

(But in real it would be still a symlinked file which originally located in its original folder)

UPDATE #1

We are working with version control system and we would like to keep the checked out folder in a global folder and we would like to symlink each folders to its proper path in the actual platform(Joomla or WordPress etc...). It would allow us to only update and commit from one folder, but still refresh every platform with a single update. (This could work until we not use FILE or DIR or any related things what symlink can mix up.)

Roland Soós
  • 3,125
  • 4
  • 36
  • 49

1 Answers1

0

It’s a pain. As the official PHP documentation explains:

The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, FILE always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

Which is a pain. This is why I have decided it’s best to set a base path explicitly as I explain here. So in your case you would set:

$BASE_PATH = '/www/';

And then your require_once would be like this:

require($BASE_PATH . '/linked/linked.php');

This question & answer is similar to yours and recommends using $_SERVER['SCRIPT_NAME'] but in my experience, that setting can change radically between server to server for odd reasons. Which is why I have defaulted to the $BASE_PATH method when I code. You set it once, forget it & no worries.

Community
  • 1
  • 1
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 1
    We are working with version control system and we would like to keep the checked out folder in a global folder and we would like to symlink each folders to its proper path in the actual platform(Joomla or WordPress etc...). It would allow us to only update and commit from one folder, but still refresh every platform with a single update. (This could work until we not use __FILE__ or __DIR__ or any related things what symlink can mix up.) – Roland Soós May 21 '14 at 12:37
  • @RolandSoós First, I would add the details you just described to your original question since it will clarify your overall need better. Do you use Capistrano? Or similar? Or are you manually symlinking? The only way I have ever been able to solve the issue of version control paths like this is to make Capistrano echo the `#{current_release}` path into a file named something like `CURRENT_PATH`. And then on the PHP side, a config file would read that `CURRENT_PATH` and use it as the `$BASE_PATH` similar to my example. – Giacomo1968 May 21 '14 at 12:41
  • 1
    Thanks, I have updated my question. No, we do not use capistrano. Also we are not symlinking as it is not working for us. This is why I'm searching for solution :) If I could solve the symlinking issue, then we would be on a good path... but no luck yet. – Roland Soós May 21 '14 at 12:54