Ben Greene
Ben Greene

Reputation: 147

How to trick PHP into thinking my absolute path would be different?

My hosting service sets my absolute path in php to "/customers/12345/BASEPATHOFMYWEBSPACE" so, i have to enter "/customers/12345/BASEPATHOFMYWEBSPACE/MYFOLDER" to get to MYFOLDER, but in html "/MYFOLDER" totally works.

because of the way my site is structured, this is a huge problem for me...

is there a way i could come up with a function, i would then include in all my php files that would trick php into accepting "/MYFOLDER" as the absolute path to MYFOLDER?

Upvotes: 0

Views: 147

Answers (2)

symcbean
symcbean

Reputation: 48387

this is a huge problem for me...

But this is just how it is - it's not specific to your hosting provider. If you can't get your head around the difference between URL paths and filesystem paths, then you're going to have lots of problems.

Chances are the webserver already knows the difference - have a look at the output of phpinfo().

If you've got your own vhost, I suspect you'll find that "/customers/12345/BASEPATHOFMYWEBSPACE" = $_SERVER["DOCUMENT_ROOT"];

So if you want to map a URL to filesystem path:

function to_file_path($url)
{
   $parts=parse_url($url);
   return $_SERVER["DOCUMENT_ROOT"] . $parts['path']
}

and conversely:

function to_url_path($file)
{
  $file=realname($file);
  if (substr($file, 0, strlen($_SERVER["DOCUMENT_ROOT"]))
        !==$_SERVER["DOCUMENT_ROOT"]) {
     return false;
  }
  return substr($file, strlen($_SERVER["DOCUMENT_ROOT"]));
}

Upvotes: 1

James Andres
James Andres

Reputation: 1532

Depending on if php safe_mode is enabled you can use ini_set() or set_include_path() to alter the include_path.

This would allow you to use relative paths after that. For example in your site's configuration (or some other file that gets included by all pages) you could do this:

<?php
// This will allow all files to have relative path access to the
// "/customers/12345/BASEPATHOFMYWEBSPACE/MYFOLDER" directory.
set_include_path(get_include_path() . PATH_SEPARATOR . "/customers/12345/BASEPATHOFMYWEBSPACE/MYFOLDER");

// Eg: now you can write require('myfile.php') and
// it will search inside of "/customers/12345/BASEPATHOFMYWEBSPACE/MYFOLDER"

Upvotes: 0

Related Questions