sevenupcan Posted July 23, 2009 Share Posted July 23, 2009 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 More sharing options...
gevans Posted July 23, 2009 Share Posted July 23, 2009 getcwd() is returning the whole path from the root installation to the current working directory, this will give you a lot of directory levels that you don't want. Link to comment https://forums.phpfreaks.com/topic/167117-some-help-with-php-paths/#findComment-881146 Share on other sites More sharing options...
Andy-H Posted July 23, 2009 Share Posted July 23, 2009 // 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... Link to comment https://forums.phpfreaks.com/topic/167117-some-help-with-php-paths/#findComment-881149 Share on other sites More sharing options...
sevenupcan Posted July 23, 2009 Author Share Posted July 23, 2009 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! Link to comment https://forums.phpfreaks.com/topic/167117-some-help-with-php-paths/#findComment-881155 Share on other sites More sharing options...
Andy-H Posted July 23, 2009 Share Posted July 23, 2009 I edited it, should work right this time. Link to comment https://forums.phpfreaks.com/topic/167117-some-help-with-php-paths/#findComment-881161 Share on other sites More sharing options...
sevenupcan Posted July 23, 2009 Author Share Posted July 23, 2009 Thanks so much. It works now. Can you explain how it works? Life SAVER! Link to comment https://forums.phpfreaks.com/topic/167117-some-help-with-php-paths/#findComment-881162 Share on other sites More sharing options...
Andy-H Posted July 23, 2009 Share Posted July 23, 2009 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. Link to comment https://forums.phpfreaks.com/topic/167117-some-help-with-php-paths/#findComment-881174 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.