2015-10-03 67 views
2

我有一个符号链接/var/www/html/lib/它指向/var/www/application/lib/解析符号链接和相对URL

在文件/var/www/application/lib/testing/imageMaker2/otherFile.php中,我创建了一个新文件/var/www/application/lib/testing/imageMaker2/images/test.png

接下来,我希望otherFile.php将相对于web服务器文档根的URL发送到客户端(即/lib/testing/imageMaker2/images/test.png)。

没有先验知识的符号链接,这怎么可能?

test.php的

<?php 
require_once('otherFile.php'); 
otherFile(__DIR__.'/images/'); //Or what ever upload directory is desired. 
? 

otherFile.php

<?php 
function otherFile($path) { 
    $path_parts = pathinfo($_SERVER['PHP_SELF']); 
    $image='test.png'; 
    echo('$path        => '.$path."\n"); 
    echo('$image        => '.$image."\n"); 
    echo('$path/$image      => '.$path.'/'.$image."\n"); 
    echo('__DIR__        => '.__DIR__."\n"); 
    echo('__FILE__       => '.__FILE__."\n"); 
    echo('$_SERVER[SCRIPT_FILENAME]   => '.$_SERVER['SCRIPT_FILENAME']."\n"); 
    echo('realpath($_SERVER[SCRIPT_FILENAME]) => '.realpath($_SERVER['SCRIPT_FILENAME'])."\n"); 
    echo('$_SERVER[PHP_SELF]     => '.$_SERVER['PHP_SELF']."\n"); 
    echo('$_SERVER[DOCUMENT_ROOT]    => '.$_SERVER['DOCUMENT_ROOT']."\n"); 
    echo('$path_parts[dirname]    => '.$path_parts['dirname']."\n"); 
    echo('dirname($_SERVER[PHP_SELF])   => '.dirname($_SERVER['PHP_SELF'])."\n"); 
} 
?> 

输出

$path        => /var/www/application/lib/testing/imageMaker2/images/ 
$image        => test.png 
$path/$image      => /var/www/application/lib/testing/imageMaker2/images//test.png 
__DIR__        => /var/www/application/lib/testing/imageMaker2 
__FILE__       => /var/www/application/lib/testing/imageMaker2/otherFile.php 
$_SERVER[SCRIPT_FILENAME]   => /var/www/html/lib/testing/imageMaker2/test.php 
realpath($_SERVER[SCRIPT_FILENAME]) => /var/www/application/lib/testing/imageMaker2/test.php 
$_SERVER[PHP_SELF]     => /lib/testing/imageMaker2/test.php 
$_SERVER[DOCUMENT_ROOT]    => /var/www/html 
$path_parts[dirname]    => /lib/testing/imageMaker2 
dirname($_SERVER[PHP_SELF])   => /lib/testing/imageMaker2 
+0

为什么不直接用'“/ lib/testing/imageMaker2 /”连接文件名的相对路径。 $ filename;'? –

+0

@JulioSoares相对路径含义'$ _SERVER ['PHP_SELF']'? – user1032531

+0

如果你想要,但你必须先提取“test.php”。我会定义一个constat。但是你甚至可以对它进行硬编码。 –

回答

0

按你的意见......

$path_parts = pathinfo($_SERVER['PHP_SELF']); 

echo $path_parts['dirname']; 

然后你只是提取你想要的。

+0

'dirname($ _ SERVER ['PHP_SELF'])'会做同样的事情。仍然需要做一些操纵使用concat或str_replace ... – user1032531

+0

但不是已经在变量中的文件名? '$ path = dirname($ _ SERVER ['PHP_SELF'])有什么不好? $ filename;'例如? –

+0

是的,'$ filename'是一个变量,它所在的路径也是如此。我原本没有做好这项工作,并编辑了原文。 – user1032531