0

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

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

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

4

3 回答 3

3

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

// ...
return $http . $name . $port . dirname($_SERVER['SCRIPT_NAME']);
于 2012-09-03T15:49:35.523 回答
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">
于 2012-09-03T15:50:22.463 回答
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 检查文件中的文件类型,与本例无关,但它确实用于输出 if(isset($mime_type[$a]))$mime_type[$a] = get_mime_type2($mime_type[$a ]);

                        $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 用于查找文件的路径,我们附加文件名,这应该会处理它。我知道应该有一种更优雅的方式来完成操作所需的内容,但是如果没有真实的案例场景来说明它可以用于什么,我只能分享我自己的实现。

这给了我一个方便的输出使用.. 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

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

于 2012-11-11T06:39:19.517 回答