Resolving Current Document Root


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function resolveDocumentRoot() {
  2. $current_script = dirname($_SERVER['SCRIPT_NAME']);
  3. $current_path = dirname($_SERVER['SCRIPT_FILENAME']);
  4.  
  5. /* work out how many folders we are away from document_root
  6.   by working out how many folders deep we are from the url.
  7.   this isn't fool proof */
  8. $adjust = explode("/", $current_script);
  9. $adjust = count($adjust)-1;
  10.  
  11. /* move up the path with ../ */
  12. $traverse = str_repeat("../", $adjust);
  13. $adjusted_path = sprintf("%s/%s", $current_path, $traverse);
  14.  
  15. /* real path expands the ../'s to the correct folder names */
  16. return realpath($adjusted_path);
  17. }

URL: http://code.arthipesa.com/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.