简体   繁体   English

如何在PHP中要求相对路径

[英]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? PHP如何在Linux服务器上从符号链接内部使用相对路径来要求文件?

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 . 因此, app/srv/www/myapp的符号链接,但是当我尝试要求使用app.config.php php感到困惑,并尝试要求/srv/www/myapp/app.config.php而不是/srv/www/accounts/dev/app.config.php

How is this possible? 这怎么可能? Thanks. 谢谢。

Try require_once(__DIR__.'../app.config.php'); 尝试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__ :) 请注意,如果您使用的是PHP <5.3,请尝试使用dirname(__FILE__)而不是__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. 我认为这是文件夹设置的方式,如果您喜欢/srv/www/accounts/dev/app.config.php。 is in a different folder then /srv/www/myapp/app.config.php 与/srv/www/myapp/app.config.php位于不同的文件夹中

ones in /accounts/dev and the other is in /myapp 一个在/ accounts / dev中,另一个在/ 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. 您可以尝试使用realpath()获取路径。 That is supposed to expand symbolic links and return the absolute path. 那应该扩展符号链接并返回绝对路径。 Something like: 就像是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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