• 0

Path to web root


Question

My current problem is that I want to be able to move my web pages about without modification and for them to still work.

At the moment I have this script:

<?php 
function path_to_root($path) 
{ 
   $pathinfo = pathinfo($path); 
   $deep = substr_count($pathinfo[dirname], "/"); 
   $path_to_root = "./";
   for($i = 1; $i <= $deep; $i++) { 
          $path_to_root .= "../";   
   } 
   return $path_to_root; 
} 

$path=path_to_root($_SERVER["PHP_SELF"]);
echo "<a href=\"".$path."index.htm">back</a>";
?>

The trouble is I can't seem to get it to work in both the root directory and a deep directory. In the root directory it puts ./../, how can I remove the ../ if it's in the root directory?

By the way this is all relative paths i.e. root/folder1/folder2

Thanks!

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

If I understood your question, try this:

<?
function path_to_root( $path )
{
   $base = pathinfo( $path );
   $deep = substr_count( $base[ 'dirname' ], "/" );

   $path_to_root = @str_repeat( "../", $deep - 1 );

   return $path_to_root;
}
?>

My comments:

EDIT:

$_SERVER[ 'PHP_SELF' ] always contains a starting "/", that's why we do $deep - 1.

So, '@' operator is useless, but it's safer (wrong parameter => $deep - 1 < 0).

Edited by Sphinx Myth
Link to comment
Share on other sites

  • 0
If I understood your question, try this:

&lt;?
function path_to_root( $path )
{
 ? $base = pathinfo( $path );
 ? $deep = substr_count( $base[ 'dirname' ], "/" );

 ? $path_to_root = @str_repeat( "../", $deep - 1 );

 ? return $path_to_root;
}
?&gt;

My comments:

We do $deep - 1 because $_SERVER[ 'PHP_SELF' ] contains a starting "/" when the script/page isn't in the root directory.

Example:

http//www.mysite.com/folder/page.php => $_SERVER[ 'PHP_SELF' ] == "/folder/page.php" => only one parent folder but two "/".

In the case where the script is in the root directory, $_SERVER[ 'PHP_SELF' ] == "page.php" => $deep == 0.

So $deep - 1 < 0. To avoid warnings, we use the '@' operator.

585750006[/snapback]

Thanks for the code but unfortunately it doesn't quite work in a deep directory. Works for the root directory fine!

Beta is the root for this example and that's where header.php is located.

Warning: main(../../header.php): failed to open stream: No such file or directory in /home/site/public_html/beta/projects/uni/php/index.php on line 13

Link to comment
Share on other sites

  • 0
Thanks for the code but unfortunately it doesn't quite work in a deep directory. Works for the root directory fine!

Beta is the root for this example and that's where header.php is located.

Warning: main(../../header.php): failed to open stream: No such file or directory in /home/site/public_html/beta/projects/uni/php/index.php on line 13

585750069[/snapback]

Ok, I made a mistake.

&lt;?
function path_to_root( $path )
{
   $base = pathinfo( $path );
   $deep = substr_count( $base[ 'dirname' ], "/" );

   $path_to_root = @str_repeat( "../", $deep == 1 ? 0 : $deep );
   /* Because pathinfo( ) will return "/" when the page is in the root directory
      and "/folder" when it's in a folder, so $deep - 1 doesn't work.
   */
   return $path_to_root;
}

But to do what you want, I'm not sure it's the best way...

Link to comment
Share on other sites

  • 0
Ok, I made a mistake.

&lt;?
function path_to_root( $path )
{
 ? $base = pathinfo( $path );
 ? $deep = substr_count( $base[ 'dirname' ], "/" );

 ? $path_to_root = @str_repeat( "../", $deep == 1 ? 0 : $deep );
 ? /* Because pathinfo( ) will return "/" when the page is in the root directory
 ? ? ?and "/folder" when it's in a folder, so $deep - 1 doesn't work.
 ? */
 ? return $path_to_root;
}

But to do what you want, I'm not sure it's the best way...

585750157[/snapback]

EDIT: Seems it won't work in a directory 1 deep such as root/folder1 for some reason it thinks it's in the root direct:pinch:; :pinch:

Thanks Sphinx thats seemed to have fixed:yes:33; :yes:

I've never come across what you have done here:

== 1 ? 0 :

Could you explain what that code does?

Edited by Syntax_Error
Link to comment
Share on other sites

  • 0
Thanks Sphinx thats seemed to have fixed it! :yes:

I've never come across what you have done here:

== 1 ? 0 :

Could you explain what that code does?

585750186[/snapback]

It's named "ternary operator". In fact, it's a compact "if ... else ..." condition.

condition ? true : false

&lt;?
$var = 0;
if( $var &amp;&amp; is_int( $var ) )
   echo "var is an int and var != 0";
else
   echo "var is null or not an int";
?&gt;

It's exactly the same as:

&lt;?
$var = 0;

echo ( $var &amp;&amp; is_int( $var ) ) ? "var is an int and var != 0" : "var is null or not an int";
?&gt;

You can also assign conditionnally a value to a var:

&lt;?
$var = $ok ? "It's OK" : "It's NOT OK";

// Exactly the same as

if( $ok )
   $var = "It's OK";
else
   $var = "It's NOT OK";
?&gt;

But there's some limits:

For example, you can't use echo in your "true" or "false" case instruction because echo is a language construct.

&lt;? condition ? echo "yes" : echo "no"; // Doesn't work ?&gt;

http://php.net/language.operators.comparison

http://php.net/echo

Link to comment
Share on other sites

  • 0

Sorry Sphinx did you see my edit? Doesn't work in a directory 1 deep from root i.e. www.site.com/folder1 just thinks it's at root :unsure:

Works at root, 2 deep and 3 deep.

Link to comment
Share on other sites

  • 0
Sorry Sphinx did you see my edit? Doesn't work in a directory 1 deep from root i.e. www.site.com/folder1 just thinks it's at root :unsure:

Works at root, 2 deep and 3 deep.

585750360[/snapback]

Damn! What a bad day! Sleeping 5 hours wasn't a good idea :pinch: .

Sorry, I didn't see your edit. This is the FINAL FIXED script:

&lt;?
function path_to_root( $path )
{
   $base = pathinfo( $path );
   /*
      pathinfo( ) returns only "/" when the page is in the root directory.
      Use $deep wasn't a good idea: "/" contains as much "/" as "/root"
   */
   $deep = $base[ 'dirname' ] == "/"
           ? 0
           : substr_count( $base[ 'dirname' ], "/" );

   $path_to_root = str_repeat( "../", $deep );   // '@' operator is useless
   return $path_to_root;
}
?&gt;

:wacko:

Edited by Sphinx Myth
Link to comment
Share on other sites

  • 0
&lt;?
function path_to_root( $path )
{
 ? $base = pathinfo( $path );
 ? /*
 ? ? ?pathinfo( ) returns only "/" when the page is in the root directory.
 ? ? ?Use $deep wasn't a good idea: "/" contains as much "/" as "/root"
 ? */
 ? $deep = $base[ 'dirname' ] == "/"
 ? ? ? ? ? ? 0
 ? ? ? ? ? : substr_count( $base[ 'dirname' ], "/" );

 ? $path_to_root = str_repeat( "../", $deep ); ? // '@' operator is useless
 ? return $path_to_root;
}
?&gt;

585750454[/snapback]

Thanks Sphinx for all you help,:Duch appreciated! :D

It all w:yes:in all directorys :yes:

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.