Posted By

jHulbert on 03/28/11


Tagged

php


Versions (?)

Get relative path to root folder


 / Published in: PHP
 

  1. function rel_path($root=NULL) {
  2. // Get script name if exists
  3. $cur_file = $_SERVER["SCRIPT_NAME"];
  4. $cur_file = explode('/', $cur_file);
  5. $cur_file = $cur_file[count($cur_file) - 1];
  6. // Remove script name
  7. $tmp = str_replace($cur_file, "", $tmp);
  8. // Split at specified root folder
  9. if($root != NULL) {
  10. $tmp = split($root, $_SERVER['REQUEST_URI']);
  11. $tmp = $tmp[1];
  12. }
  13. // Make windows-friendly
  14. $tmp = str_replace('\\', '/', $tmp);
  15. // Create path array
  16. $tmp = explode('/', $tmp);
  17.  
  18. $rel_path = NULL;
  19. for ($i = 0; $i < count($tmp); $i++) {
  20. if ($tmp[$i] != '') {
  21. $rel_path .= '../';
  22. }
  23. }
  24.  
  25. if ($rel_path != NULL) {
  26. $rel_path = substr($rel_path, 0, -1);
  27. } else {
  28. $rel_path = '.';
  29. }
  30.  
  31. return $rel_path;
  32. }

Report this snippet  

You need to login to post a comment.