Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the original path of a symbolic link in PHP?

Tags:

path

php

symlink

I have several files in my web folder, including these two:

  • /mydocroot/include/somesubdir/include.inc.php
  • /mydocroot/include/settings.inc.php

where somesubdir is a symbolic link to another directory on my disk, to a path named, eh, let's say /anywhere/else.

Inside include.inc.php, something like this is written:

<?php

require_once "../settings.inc.php";

?>

In my opinion, that should include /mydocroot/include/settings.php... But guess what happens: PHP tries to include /anywhere/settings.inc.php, instead of /mydocroot/include/settings.inc.php.

It seems like PHP automatically resolves symbolic links.

How can I avoid this and include my settings.inc.php file?

like image 264
MC Emperor Avatar asked Mar 16 '12 12:03

MC Emperor


People also ask

How do I restore a symbolic link?

There is no command to retarget a symbolic link, all you can do is remove it and create another one. The rename command's -s option will "rename a symbolic link target, not the symbolic links itself". It will also operate on multiple symbolic links at once.

How can I get full image path in PHP?

<input type="file" name="upload_captcha_background" id="upload_captcha_background" /> var file_path= jQuery("#upload_captcha_background"). val(); alert(file_path);


1 Answers

I just had a similar issue. You can do it by using $_SERVER['SCRIPT_FILENAME'] variable, that displays the requested file, so in your case it will be /mydocroot/include/somesubdir/include.inc.php, even if somesubdir is a symbolic link. To include a file that is one level lower instead of doing

require_once "../settings.inc.php";

do this:

require_once dirname(dirname($_SERVER['SCRIPT_FILENAME'])).DIRECTORY_SEPARATOR."settings.inc.php"

Documentation about $_SERVER variables

like image 108
arseniew Avatar answered Nov 15 '22 15:11

arseniew