Jump to content

Some help with PHP paths?


sevenupcan

Recommended Posts

Hi I wondered if someone could help me understand the following piece of code...

 

// THIS is the root directory
define('ROOT', getcwd());

// make some URL's
$url = new stdClass();
$url->root = ROOT;
$url->base = 'http://' . $_SERVER['HTTP_HOST'] . str_replace($_SERVER['PHP_SELF'], '', ROOT);

 

The URL which is returned ends up as...

 

http://sevenupcan.com/nfs/c01/h09/mnt/488/domains/sevenupcan.com/html/stage/seamlessmedia/

 

However what it should be is...

http://sevenupcan.com/stage/seamlessmedia/

 

If I do...

<?php
echo getcwd();
?>

 

I get...

 

/nfs/c01/h09/mnt/488/domains/sevenupcan.com/html/stage/seamlessmedia

 

Please any help would be greatful, I don't know much about PHP.

 

Link to comment
https://forums.phpfreaks.com/topic/167117-some-help-with-php-paths/
Share on other sites

// THIS is the root directory
define('ROOT', getcwd());

// make some URL's
$url = new stdClass();
$url->root = ROOT;
$url->base = 'http://' . $_SERVER['SERVER_NAME'] . substr(substr(ROOT, strpos(ROOT, 'html')), 4);

 

Does that work ok?

 

//Edited code...

gevans - that's what I was thinking but it seems to work on my other hosts but Media Temple's not liking it so I was trying to understand exactly how it works.

 

Andy-H - This does seem to do something but it looks like the html part is getting in the way...

 

This is what it produces...

 

http://sevenupcan.comhtml/stage/seamlessmedia/

 

Should output...

 

http://sevenupcan.com/stage/seamlessmedia/

 

Scrap that, I must have missed the 4 and it now works. Can how it does that?

 

Many thanks for your help. Your a LIVE SAVER!

I'm sure it isn't the most efficient solution but just the first that came to mind.

 

If you look into the substr and strpos you will see that it takes the current working directory (getcwd) or 'ROOT' as your code defined.

 

It then uses the sub-string function and searches for the string position (strpos) of html to cut to the part after (and including) the 'html' part of the cwd.

 

It then uses the sub-string function (again) to take away the first four letters(html) of the string returned and add the remainder of the string to the SERVER_NAME (domain) stored in the built-in $_SERVER array.

 

I understand that this is hardly a decent explanation, if you have any questions of further details feel free to ask.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.