简体   繁体   中英

How to require a relative path in PHP

How can I PHP require a file using a relative path on a Linux server from inside a symbolic link?

I tried:

require_once(dirname(__DIR__) . "/app.config.php");

But that is throwing an error saying the file does not exist. Here is the file structure:

/srv/www/accounts/dev
   app => /srv/www/myapp
       index.php
   app.config.php

So app is a symbolic link to /srv/www/myapp but when I try and require app.config.php php get's confused and tries to require /srv/www/myapp/app.config.php instead of /srv/www/accounts/dev/app.config.php .

How is this possible? Thanks.

Try require_once(__DIR__.'../app.config.php');

Hope that works :)

Just a note, if you're on PHP < 5.3, try dirname(__FILE__) instead of __DIR__ :)

require_once(__ DIR __。“ /../ app.config.php”);

i think its the way the folders are setup, if u have like the /srv/www/accounts/dev/app.config.php. is in a different folder then /srv/www/myapp/app.config.php

ones in /accounts/dev and the other is in /myapp

you dont want a relative path you want absolute, relative would only be in that folder you have to at least let it know which folder to check, so i think you wanna go to the a different folder

You might try using realpath() to get the path. That is supposed to expand symbolic links and return the absolute path. Something like:

require_once(realpath('../') . "app.config.php");

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