English 中文(简体)
2. 父母管理书
原标题:Get parent directory of running script

在PHP中,获得、<>/strong>的当前版本的目录与www.gen有关的最佳途径是什么? 我:

$_SERVER[ SCRIPT_NAME ] ==  /relative/path/to/script/index.php 

或者说:

$something_else ==  /relative/path/to/script/ 

并且,我需要打上适当插入<条码>/rel/path/to/。 你会提出什么建议? 选择一名线人。

http://www.ohchr.org。

我需要走一条与www fundamental,dirname (__FILE__)相对的道路,使我成为档案系统中的一条绝对道路,从而赢得t工作。 http://www.ohchr.org。

最佳回答

如果您的文字载于/var/www/dir/index.php。 之后,将返回:

dirname(__FILE__); // /var/www/dir

dirname( dirname(__FILE__) ); // /var/www

Edit

This is a technique used in many framew或ks to determine relative paths from the app_root.

档案结构:

 /var/
      www/
          index.php
          subdir/
                 library.php

指数。 php是我的发送者/船舶陷阱文件,所有要求都通过:

define(ROOT_PATH, dirname(__FILE__) ); // /var/www

library.php is some file located an extra direct或y down and I need to determine the path relative to the app root (/var/www/).

$path_current = dirname( __FILE__ ); // /var/www/subdir
$path_relative = str_replace(ROOT_PATH,   , $path_current); // /subdir

计算相对路径的方法可能更好,然后是str_replace(),但你可以这样做。

问题回答

如PHP 5.3.0,可为此目的使用<代码>DIR__。

The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__ FILE__).

见http://www.php.net/manual/en/language.constants.prefin.php。 PHPmagtants

C:www>php --version
PHP 5.5.6 (cli) (built: Nov 12 2013 11:33:44)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies

C:www>php -r "echo __DIR__;"
C:www

2. 领取现稿的父母。

$parent_dir = dirname(__DIR__);

如果我正确理解你的问题,那么你手写的描述就是

/relative/path/to/script/index.php

这将使你在文件www.:

$parent_dir = dirname(dirname($_SERVER[ SCRIPT_NAME ])) .  / ;
//$parent_dir will be  /relative/path/to/ 

如果你想到你有关服务器根基的手稿的家长名录:

$parent_dir = dirname(dirname($_SERVER[ SCRIPT_FILENAME ])) .  / ;
//$parent_dir will be  /root/some/path/relative/path/to/ 

我希望这将有助于你。

echo getcwd(). <br> ; // getcwd() will return current working directory
echo dirname(getcwd(),1). <br> ;
echo dirname(getcwd(),2). <br> ;
echo dirname(getcwd(),3). <br> ;

产出:

C:wamp64wwwpublic_htmlstep
C:wamp64wwwpublic_html
C:wamp64www
C:wamp64

很幸运的是,这将做到:

substr($_SERVER[ SCRIPT_NAME ], 0, strpos($_SERVER[ SCRIPT_NAME ],basename($_SERVER[ SCRIPT_NAME ])))
$dir = dirname($file) . DIRECTORY_SEPARATOR;

这里是我所利用的,因为我不是一帆风顺的。

function getCurrentOrParentDirectory($type= current )
{
    if ($type ==  current ) {
        $path = dirname(__FILE__);  
    } else {
        $path = dirname(dirname(__FILE__));
    }
    $position = strrpos($path,  / ) + 1;
    return substr($path, $position);
}

母系的“@mike b”推荐的双母子,目前目录仅用该辛子一度。

<>Note this function only Back the 不结盟运动E, slashes must be supplemented after.

这是我使用的一项职能。 创立它后,我总是具备这一功能:

function getDir(){
    $directory = dirname(__FILE__);
    $directory = explode("/",$directory);
    $findTarget = 0;
    $targetPath = "";
    foreach($directory as $dir){
        if($findTarget == 1){
            $targetPath = "".$targetPath."/".$dir."";
        }
        if($dir == "public_html"){
            $findTarget = 1;
        }
    }
    return "http://www.".$_SERVER[ SERVER_NAME ]."".$targetPath."";
}

引证。 Works on bothwinds or linux services.

str_replace( , / ,dirname (dirname (__FILE__))

这也是一种可能的解决办法。

$relative =  /relative/path/to/script/ ;
$absolute = __DIR__.  /../  .$relative;

我希望,这将有助于我的工作。

function get_directory(){
    $s = empty($_SERVER["HTTPS"]) ?    : ($_SERVER["HTTPS"] == "on") ? "s" : "";
    $protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol . "://" . $_SERVER[ SERVER_NAME ] . dirname($_SERVER[ PHP_SELF ]);
}
define("ROOT_PATH", get_directory()."/" );
echo ROOT_PATH;




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...