【问题标题】:Resolving symbolic links and relative URLs解析符号链接和相对 URL
【发布时间】:2015-10-04 00:34:10
【问题描述】:

我有一个符号链接/var/www/html/lib/,它指向/var/www/application/lib/

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

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

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

test.php

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

其他文件.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

【问题讨论】:

  • 你为什么不把相对路径和"/lib/testing/imageMaker2/". $filename;这样的文件名连接起来?
  • @JulioSoares 相对路径含义$_SERVER['PHP_SELF']?
  • 如果你愿意,你必须先提取“test.php”。我会改为定义一个 const 。但您甚至可以对其进行硬编码。
  • 感谢 Julio,但我的目的不是硬编码或定义硬编码常量。

标签: php symlink


【解决方案1】:

根据您的 cmets...

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

echo $path_parts['dirname'];

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

【讨论】:

  • dirname($_SERVER['PHP_SELF']) 也会这样做。仍然需要使用 concat 或 str_replace 进行一些操作...
  • 但是文件名不是已经在变量中了吗?例如,$path = dirname($_SERVER['PHP_SELF']) . $filename; 有什么不好?
  • 是的,$filename 是一个变量,它所在的路径也是。我最初没有很好地展示这一点并编辑了原始帖子。
  • 那你为什么不这样做呢?
  • 做什么?编辑原帖?我做到了。在otherFile($path) 中,我需要想出/lib/testing/imageMaker2/images/test.png 其中$path 是任何有效的公共文件路径。
猜你喜欢
  • 1970-01-01
  • 2015-02-22
  • 2011-06-14
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
相关资源
最近更新 更多