Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP __DIR__ or __FILE__ symlinked [duplicate]

Tags:

php

Using __DIR__ and __FILE__ constants do not work in a symlinked situation. What is the workaround for this?

For example:

I have a file:

/home/me/modules/myfile.php

It is symlinked to:

/var/www/project/app/myfile.php

from in /home/me/modules/myfile.php i need to include a file that is located in /var/www/project

EDIT

To the suggestions of using realpath() - unfortunately this does not work.

var_dump(__DIR__);
var_dump(realpath(__DIR__));

both return exactly the same filepath

like image 793
Marty Wallace Avatar asked Dec 07 '12 21:12

Marty Wallace


People also ask

How to resolve symlinks in PHP?

The PHP interpreter resolves symlinks before it processes them. You can do this yourself with the readlink function. PHP resolves the links because it's more efficient for *_once functions and code caches like APC, Xcache etc.

How to run two files in a directory in PHP?

Consider having two files in a directory called inc, which is a subfolder of our project's directory, where the index.php file lies − from the index.php, it runs successfully.

What is the function of dirname () in PHP?

PHP dirname() Function 1 Definition and Usage. The dirname () function returns the path of the parent directory. 2 Syntax 3 Parameter Values 4 Technical Details. Yes, in PHP 5.0

What does the function dirname (path) return?

The dirname() function returns the path of the parent directory. Syntax dirname(path, levels) Parameter Values Parameter Description path Required. Specifies a path to check levels Optional. An integer that specifies the number of parent directories to go up.


1 Answers

Have you tried the following?

dirname($_SERVER['SCRIPT_FILENAME'])

In your case this will return: /var/www/project/app

like image 71
Tim Avatar answered Oct 17 '22 01:10

Tim