【问题标题】:Getting relative path from folder从文件夹获取相对路径
【发布时间】:2014-08-03 17:12:27
【问题描述】:

我的文件管理器文件夹位于此处:

localhost/cakeapp/app/webroot/js/tinymce/js/tinymce/plugins/filemanager/

在 cakeapp 根目录下,我有两个文件夹:

uploadedImages (cakeapp/uploadedImages)
UploadedImagesThumbs (cakeapp/uploadedImagesThumbs).

我在config.php文件中有这种情况:

$base_url ="http://localhost";

// path from base_url to base of upload folder (with start and final /)
$upload_dir = '/cakeapp/uploadedImages/';

// relative path from filemanager folder to thumbs folder (with final /)
$thumbs_base_path = '???????/uploadedImagesThumbs'; 

// relative path from filemanager folder to upload folder (with final /)
$current_path = '???????/uploadedImages/'; 

如何从 filemanager 文件夹中获取 $thumbs_base_path$current_path 的正确相对路径?

【问题讨论】:

  • $thumbs_base_path = '../../../../../../../../uploadedImagesThumbs/';?
  • 谢谢,它有效!当我试图解决这个问题时,我做了七次'../'而不是八次。
  • 这看起来不像是好的编码......

标签: php relative-path absolute-path


【解决方案1】:

你可以试试

function getRelativePath($from, $to) {
$patha = explode('/', $from);
$pathb = explode('/', $to);
$start_point = count(array_intersect($patha,$pathb));
while($start_point--) {
    array_shift($patha);
    array_shift($pathb);
}
$output = "";
if(($back_count = count($patha))) {
    while($back_count--) {
        $output .= "../";
    }
} else {
    $output .= './';
}
return $output . implode('/', $pathb);
}

查看source。它还有更多示例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 2020-10-31
    • 2018-11-11
    • 2017-11-03
    • 1970-01-01
    相关资源
    最近更新 更多