iLuke Posted August 23, 2011 Share Posted August 23, 2011 Hi guys, Just a quick one - this is more of a bit of a challenge than a problem! I'm pretty good with PHP - I'm definitely not a beginner, but I've come across a problem I've not yet been able to solve satisfactorily. Essentially, I am trying to write a really good, solid OOP library that I can hand out for people to use for CMS-driven websites, and I want to literally just upload some files and have access to some variables... simple enough. But, my snagging point came when I was writing the image library, which handles uploading files and moving them from place to place. These functions as you know usually require an absolute server path - not a relative path and certainly not a URL, so I wanted to place something in my config file that gets the base URL, puts the relative path in one variable (e.g. mysite.com/directory would give /directory) and the server path in another (e.g. mysite.com/directory might give var/www/mysite/www/directory). Now I might just be being foolish and overlooking something, but I've used HTTP_HOST, DOCUMENT_ROOT etc to get this information, but I only want to get the base directory that the application resides in - usually with installed software sure as WordPress you have to specify which directory the files are going to sit in, which I wondered if you could gauge automatically? Any help would be appreciated - I've tried as many clever approaches as I could think of, and the only one I can think of now is setting the variables based on, say, installtion.php since I know the name of the file and where it resides, I can strip out all folders minus the filename, which would give me the root directory, but that seems a little fiddly to me? Someone must have done this before and have a nice clean solution to the problem? I'm at a loss anyway, and I figured it might be a fair challenge to do? Thanks, Luke Quote Link to comment Share on other sites More sharing options...
Adam Posted August 23, 2011 Share Posted August 23, 2011 Your library should not decide where an application wants to store images, it should be passed that information. Suppose someone wants to use it for two completely different sites on the same server, and have a central location where they keep it. If your library decides all this information then it's not re-usable for both cases. Keep it generic, and move the logic into the application. Edit You should know the directory structure of your application, so you can pass a relative URL to realpath to get the abolsute path. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 23, 2011 Share Posted August 23, 2011 If you have a "core" / "controller" file that is run for every page then there is a trick you can use. $ROOT_PATH = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR; This will set $ROOT_PATH to the location of the file that runs that line of code. Then, you can create variables to different folders used by the application that you can use throughout the application: $_DS_ = DIRECTORY_SEPARATOR; $_PATHS['modules'] = $_PATHS['root'] . 'modules' . $_DS_; $_PATHS['classes'] = $_PATHS['root'] . 'classes' . $_DS_; $_PATHS['templates'] = $_PATHS['root'] . 'templates' . $_DS_; Although I agree with Adam that the location should be configurable. Quote Link to comment Share on other sites More sharing options...
Nedals Posted August 23, 2011 Share Posted August 23, 2011 I am by no means a PHP guru, so take this for what its worth. $ROOT_PATH = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR; There appears to be a probem with this method when you move away from the WEBROOT directory. Given: /home/site/htdocs/index.php or other pages in this directory $ROOT_PATH will be '/home/site/' /home/site/htdocs/member/menu.php or other pages in the member directory $ROOT_PATH will now be '/home/site/htdocs/' So your ROOT_PATH changes depending on the directory where the page resides. Not what you want. I use: $ROOT_PATH = preg_replace('!^(.+?)/\w+/?$!i', '$1', getenv('DOCUMENT_ROOT')); This is unchanged regardless of the directory. This method will also work across servers with different $ROOT_PATH's as long as the website directory structure remains the same. You will likely need to tweek the regexp for a Micro$ server. $ROOT_PATH = preg_replace('!^(.+?)(\\|/)\w+(\\|/)?$!i', '$1', getenv('DOCUMENT_ROOT')); (maybe! - untested) Quote Link to comment Share on other sites More sharing options...
jcbones Posted August 24, 2011 Share Posted August 24, 2011 So your ROOT_PATH changes depending on the directory where the page resides. Not what you want. This will set $ROOT_PATH to the location of the file that runs that line of code. Yep, I think that is exactly what Mj wants. If the page was 4 sub directories deep, getting the web_root directory wouldn't do that page a bit of good. Therefore, you always need to make sure the page knows EXACTLY where it resides. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 24, 2011 Share Posted August 24, 2011 I am by no means a PHP guru, so take this for what its worth. . . . So your ROOT_PATH changes depending on the directory where the page resides. Not what you want. Duh. Yeah. It has nothing to do with being a "PHP Guru", it's called paying attention to detail. Did you not read the VERY FIRST sentence in my reply? If you have a "core" / "controller" file that is run for every page then there is a trick you can use. You run that line in the controller file. For just about any application you need a single file to set configuration data and/or make database connections etc. For many of my sites my "pages" do nothing more than set a few variables and then load the index.php file at the root of the site. That page uses those variables to then determine what logic pages to load (which actually reside outside the public folder so none of my files with any core logic are publicly accessible). By placing that line of code in the root of the web accessible file it sets the root directory one level up. So, I can then define the paths to the logic files outside the web root. By using the method above I can easily change the folder structure of the site and then simply change one line and all the pages that reference those paths will work without having to search and replace throughout all the files. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.