php 获取绝对文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10122859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:26:18  来源:igfitidea点击:

Get an absolute file path

phpfilepathabsolute-path

提问by vvr

How to get a absolute file path php

php如何获取绝对文件路径

I have a folder abc and xyz.

我有一个文件夹 abc 和 xyz。

I am including the file a.php of abc in xyz folder using Ajax request by giving relative path which is like:

我使用 Ajax 请求将 abc 的文件 a.php 包含在 xyz 文件夹中,方法是提供如下所示的相对路径:

../a.php

The file a.php contains some actions which are done using Ajax request.

文件 a.php 包含一些使用 Ajax 请求完成的操作。

In xyz folder i want to perform same actions which are perform in abc folder, but when i try to perform those actions it is searching for files in xyz folder instead of abc, so the actions which i want to perform in xyz are not working.

在 xyz 文件夹中,我想执行在 abc 文件夹中执行的相同操作,但是当我尝试执行这些操作时,它正在 xyz 文件夹而不是 abc 中搜索文件,因此我想在 xyz 中执行的操作不起作用。

Please help me how to do this.

请帮助我如何做到这一点。

Updated code:

更新代码:

$(function(){
    $.ajax({
        type:"POST",
        url: "../xyz/a.php",
        data: {
            "Id": '<?php echo $_GET['Id'];?>'
        },
        success: function(data){
            $("#divId").html(data);
        }
    }); 
});

回答by smassey

I recommend you do something like this in your config/index/bootstrap:

我建议你在你的 config/index/bootstrap 中做这样的事情:

define('ROOT_DIRECTORY', dirname(__FILE__).'/');

This way when you need to load files, from other locations, you make all the paths relative to the ROOT_DIRECTORY. for example:

这样,当您需要从其他位置加载文件时,您可以创建相对于 ROOT_DIRECTORY 的所有路径。例如:

require_once(ROOT_DIRECTORY . 'abc/xyz.php');

This will make file inclusions a LOT simpler and will allow you to move the entire project directory to another location ( production server for example ) without breaking any 'path' logic.

这将使文件包含更简单,并允许您将整个项目目录移动到另一个位置(例如生产服务器)而不会破坏任何“路径”逻辑。

Seeing your code update, I see you're really talking about the request from the javascript.. in that case just leave the full url to the file: url:'http://server.com/xyx/abc.php'

看到你的代码更新,我看到你真的在谈论来自 javascript 的请求。

Cheers

干杯

回答by T.Todua

For example, you are in http://your-site.com/folder1/folder2/file.php.

例如,您在http://your-site.com/folder1/folder2/file.php 中

here are php variables:

这是php变量:

==============================================================================

    ======I======
    __File__;                           C:\wamp\www\folder1\folder2\file.php
    $_SERVER['PHP_SELF'];               /folder1/folder2/file.php
    //$_SERVER['PHP_SELF'] is the same as $_SERVER["REQUEST_URI"];

    ======II======
    getcwd();                           C:\wamp\www\folder1\folder2\
    dirname();                          OUTPUTS NOTHING - EMPTY NOT ALLOWED
    basename();                         OUTPUTS NOTHING - EMPTY NOT ALLOWED
    __dir__;                            C:\wamp\www\folder1\folder2

    ======III======
    getcwd( XXX );                      OUTPUTS NOTHING - PARAMETER NOT ALLOWED
    getcwd( XXX );                      OUTPUTS NOTHING - PARAMETER NOT ALLOWED
    getcwd( XXX );                      OUTPUTS NOTHING - PARAMETER NOT ALLOWED

    dirname(__FILE__);                  C:\wamp\www\folder1\folder2
    dirname($_SERVER['PHP_SELF']);      /folder1/folder2
    dirname(getcwd());                  C:\wamp\www\folder1
    dirname(dirname());                 OUTPUTS NOTHING - EMPTY NOT ALLOWED
    dirname(basename());                OUTPUTS NOTHING - EMPTY NOT ALLOWED

    basename(__FILE__);                 file.php
    basename($_SERVER['PHP_SELF']);     file.php
    basename(getcwd());                 folder2
    basename(dirname());                OUTPUTS NOTHING - EMPTY NOT ALLOWED
    basename(basename());               OUTPUTS NOTHING - EMPTY NOT ALLOWED

    ======IV======
    on dirname
    dirname(dirname(__FILE__));                     C:\wamp\www\folder1
    dirname(dirname($_SERVER['PHP_SELF']));         /folder1
    dirname(dirname(getcwd()));                     C:\wamp\www
    basename(dirname(__FILE__));                    folder2
    basename(dirname($_SERVER['PHP_SELF']));        folder2
    basename(dirname(getcwd()));                    folder1;

    on basename
    dirname(basename(__FILE__));                    .
    dirname(basename($_SERVER['PHP_SELF']));        .
    dirname(basename(getcwd()));                    .
    basename(basename(__FILE__));                   file.php
    basename(basename($_SERVER['PHP_SELF']));       file.php
    basename(basename(getcwd()));                   folder2
==============================================================================

============EXAMPLES===========

============示例============

Home url

主页网址

<?php echo $_SERVER['HTTP_HOST'];?>

ONLY current FILE url (like: mysite.com/myfile.php)

仅当前文件 url(如:mysite.com/myfile.php)

<?php echo $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; ?>

Current url Fully (like: mysite.com/myfile.php?action=blabla

当前网址完全(如:mysite.com/myfile.php?action=blabla

<?php echo $_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];?>

To get RealPath to the file (even if it is included) (change /var/public_html to your desired root)

获取文件的 RealPath(即使包含它)(将 /var/public_html 更改为您想要的根目录)

<?php
// First step: Get full path
$cur_file=str_replace('\','/',__FILE__);
// Second step: Remove the root path
$cur_file=preg_replace('/(.*?)\/var\/public_html/','',$cur_file);
?>

for wordpress, there exist already pre-defined functions to get plugins or themes url.

对于 wordpress,已经存在预定义的函数来获取插件或主题 url。

回答by vvr

In Ajax request i have changed url like this

在 Ajax 请求中,我更改了这样的 url

url: location.protocol + "//" + location.host + "projectname/foldername/filename.php"

Then it worked perfectly

然后它完美地工作

回答by Andris

Firstly, if you want to include a file in xyzfrom abc, you need to do ../abc/a.php, not ../a.php(unless, of course, the file is on the same level as your both directories).

首先,如果您想在xyzfrom 中包含文件abc,则需要执行../abc/a.php,而不是../a.php(当然,除非该文件与您的两个目录处于同一级别)。

Secondly, the function to convert relative paths to absolute is called realpathand you can find the documentation here.

其次,调用将相对路径转换为绝对路径的函数realpath,您可以在此处找到文档。

If your directory structure was something like this:

如果您的目录结构是这样的:

abc/
xyz/
a.php

...then to get the absolute path of a.phpfrom a file located in one of your directories, you would do

...然后a.php要从位于您的目录之一中的文件中获取绝对路径,您可以这样做

$absolute = realpath('../a.php');