获取服务器PHP上的当前目录

时间:2012-09-03 15:45:48

标签: php

我有以下用于获取服务器信息的函数:

function srv_name(){
if ($_SERVER['SERVER_PORT'] != 80) {
    $port = ':'.$_SERVER['SERVER_PORT'];
} else {
    $port = '';
}
$name = $_SERVER['SERVER_NAME'];
if (isset($_SERVER['HTTPS'])) {
    $http = 'https://';
} else {
    $http = 'http://';
}
return $http.$name.$port;
}

我使用如下:

<link type="text/css" rel="stylesheet" href="<?php echo srv_name(); ?>/css/styles.css">

如果php文件位于Web服务器的根目录中,则可以正常工作,例如:mydomain.com/index.php 但如果文件位于子目录中,则不起作用,例如:mydomain.com/mydir/index.php

我需要在函数中添加什么来获取子目录?

3 个答案:

答案 0 :(得分:2)

如果模板文件和css目录在同一目录中,即

mydomain.com/templates/mytemplate/index.php --> template
mydomain.com/templates/mytemplate/css/styles.css --> stylesheet in css direcory

然后使用

<link type="text/css" rel="stylesheet" href="css/styles.css">

答案 1 :(得分:1)

您可以添加dirname($_SERVER['SCRIPT_NAME'])

// ...
return $http . $name . $port . dirname($_SERVER['SCRIPT_NAME']);

答案 2 :(得分:0)

我正在使用类似下面的代码来检索文件的路径,稍后我们将其用作php中电子邮件消息类的输出。

                 //To this folder I'm uploading in another module

$folder='uploads/';


        //get the server address.. it's a vpn accessible ip in our case 
        $dev_baseurl= 'http://192.168.xxx.xyz'; 

//在这种情况下对我有用的功能*(如果有更好的方式可以随意添加评论)

                    function check_address($current_dir,$https_check)
                    {
//$https_check, user defined var for
$conflen=strlen($current_dir);
//im using this and don't ask me why :P
$conflen1=0;

$B=substr(__FILE__,0,strrpos(__FILE__,'/'));
$A=substr($_SERVER['DOCUMENT_ROOT'], strrpos($_SERVER['DOCUMENT_ROOT'], $_SERVER['PHP_SELF']));
$C=substr($B,strlen($A));

$posconf=strlen($C)-$conflen1-1;
$D=substr($C,1,$posconf);

if (isset($_SERVER['HTTPS'])) $https_check=1;

switch($https_check)
{
case 0:
            $host='http://'.$_SERVER['SERVER_NAME'].'/' .$D.'';
break;

case 1:
            $host='https://'.$_SERVER['SERVER_NAME'].'/'.$D.'';
break;
}
//output the address
return $host;


 }

尝试获取远程托管文件的路径

$dev2_url=check_address('/',1);

$dev2_url="{$dev2_url}";

/* That should be the output we are looking for 
   $dev_url="{$dev_baseurl}".str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname($_SERVER['SCRIPT_FILENAME']))."/{$folder}";
*/ 

......我的php中的其他一些逻辑

     //$body is a message string that we are sending with a class call as a parameter later
    foreach( $_POST['attachments'] as $a => $avalue)
{
    if (is_file($avalue))
     {  

      $mime_type[$a] = substr(strrchr($avalue, '.'), 1);


    $handle=fopen($avalue, 'rb');
                                      $bs_name=basename($avalue);
                                      $filesize=filesize($avalue);
                                      $f_contents=fread($handle, $filesize);
                                      fclose($handle);

// get_mime_type2检查文件中的文件类型,与此示​​例无关,但它对输出有效,但是(isset($ mime_type [$ a]))$ mime_type [$ a] = get_mime_type2($ mime_type) [$一]);

                        $body.= '<li><ul><li>Filename:&nbsp;' . $bs_name .$eol;
                        $body.= '<li>FileType:&nbsp;'.$mime_type[$a].$eol;

                                            //In this line I use the calculated path to pass it as an output to an HTML based version of an email
                                            $body.= "<li>Path:&nbsp;<a href=\"{$dev2_url}/{$avalue}\" title=\"Open w/ browser {FileName: $bs_name} | Size: {$filesize}\" target=\"_parent\">"  .$avalue . "</a><li>Approx.File-Size:{$filesize} bytes</ul>";


                                    }
    }  
$body.='</ul>';     

请注意这一行: ...

$body.= "<li>Path:&nbsp;<a href=\"{$dev2_url}/{$avalue}\" title=\"Open w/ browser {FileName: $bs_name} | Size: {$filesize}\" target=\"_parent\">"  .$avalue . "</a><li>Approx.File-Size:{$filesize} bytes</ul>";    

.. $ dev2_url用于查找文件的路径,我们附加文件名,应该处理它。我知道应该有一种更优雅的方式来完成op需要的东西,但是如果没有它可以用于什么的真实案例场景我只能分享我自己的实现。

这给了我一个方便的输出使用.. IE:

//http://192.168.xx.xyZ is actually a real IP address

http://192.168.xx.xyZ/ProjectX/protos/jquery-menu2/includes/uploads/t_1.txt

用于附加到电子邮件消息的每个文件的输出