doubledee Posted November 25, 2011 Share Posted November 25, 2011 I just can't seem to get *paths* down?! Here is my Web Root structure... components body_footer.inc.php pages article_index.php index.php In the file article_index.php, I have this code which doesn't work... <!-- BODY FOOTER --> <?php require_once('components/body_footer.inc.php'); ?> </body> I changed it to this... <!-- BODY FOOTER --> <?php require_once('/components/body_footer.inc.php'); ?> </body> ...but it still doesn't work?! If I start off the include with a forward-slash, in my mind that says "Start at the Web Root and go find 'body_footer.inc.php' inside the 'components' directory. Why doesn't that work?! Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 25, 2011 Share Posted November 25, 2011 let's get the obvious question out of the way, have you already required this file before using require_once? Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 25, 2011 Share Posted November 25, 2011 What does "It doesn't work" mean? Error message? No content being shown without an error message? Have you checked "component's" permissions? Quote Link to comment Share on other sites More sharing options...
doubledee Posted November 25, 2011 Author Share Posted November 25, 2011 let's get the obvious question out of the way, have you already required this file before using require_once? Not in the page we are talking about. Debbie Quote Link to comment Share on other sites More sharing options...
doubledee Posted November 25, 2011 Author Share Posted November 25, 2011 What does "It doesn't work" mean? Error message? No content being shown without an error message? Have you checked "component's" permissions? It means... Warning: require_once(/components/body_footer.inc.php) [function.require-once]: failed to open stream: No such file or directory in /Users/user1/Documents/DEV/++htdocs/05_Debbie/pages/article_index.php on line 41 Debbie Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 25, 2011 Share Posted November 25, 2011 the "/" is pointing to the base of the directory that the calling page is in, in this case it would be calling the "pages" directory. your can use ./ to go back a directory from the one your are in now. ?php require_once('./components/body_footer.inc.php'); ?> Quote Link to comment Share on other sites More sharing options...
doubledee Posted November 25, 2011 Author Share Posted November 25, 2011 the "/" is pointing to the base of the directory that the calling page is in, in this case it would be calling the "pages" directory. your can use ./ to go back a directory from the one your are in now. <?php require_once('./components/body_footer.inc.php'); ?> If I use this it works... <?php require_once('../components/body_footer.inc.php'); ?> Why is it that using a forward slash (/) works fine with HTML references, but not PHP? (I thought the logic was the same?) Debbie Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 25, 2011 Share Posted November 25, 2011 the "/" is pointing to the base of the directory that the calling page is in, in this case it would be calling the "pages" directory. your can use ./ to go back a directory from the one your are in now. ?php require_once('./components/body_footer.inc.php'); ?> What? Using / in a filesystem function points to the root of the filesystem, not the current directory. Preceding the slash with a dot points to the current directory. Preceding it with double dot points to the directory one level back. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 25, 2011 Share Posted November 25, 2011 meant to write ../ i also was to believe that / points to the root directory, however her results state otherwise, unless the directory that she is trying to call is not in the root directory, however from the data given, it appears that it is. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 25, 2011 Share Posted November 25, 2011 The error clearly shows that require() is looking for the file in the '/components/' directory, which would be from the / root of the filesystem. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 25, 2011 Share Posted November 25, 2011 what i see in the error is the file that she searched for, and the file returning the error. Quote Link to comment Share on other sites More sharing options...
doubledee Posted November 26, 2011 Author Share Posted November 26, 2011 So what's going on gang? I'm confused *again* on this entire topic... *sigh* This summer I went to great lengths to create a "config" file that handled all of this and pointed everything back to "root", but people kept telling me that I didn't need to do that going from my Dev environment to Production. So this Fall I learned how to create a "virtual host" on my Dev Laptop. It seems to work great, but I am re-factoring my code and trying to clean things up, and this error makes me think I don't understand how paths work again?! Debbie Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 26, 2011 Share Posted November 26, 2011 Does Apache have its document root set properly? Quote Link to comment Share on other sites More sharing options...
doubledee Posted November 26, 2011 Author Share Posted November 26, 2011 Does Apache have its document root set properly? MAMP has for the last few years... This isn't a webserver issue, it is me not understanding what path symbols to use when and where. Debbie Quote Link to comment Share on other sites More sharing options...
kicken Posted November 26, 2011 Share Posted November 26, 2011 i also was to believe that / points to the root directory, however her results state otherwise, unless the directory that she is trying to call is not in the root directory, however from the data given, it appears that it is. Web Root is not the same as the Filesystem root. The webroot is whatever the webserver is configured to use as their document root, eg $_SERVER['DOCUMENT_ROOT']. The Filesystem root is the root of the filesystem/drive (eg '/' on linux, C:\ (or relevant drive) on windows). Why is it that using a forward slash (/) works fine with HTML references, but not PHP? (I thought the logic was the same?) PHP paths are based on the Filesystem layout. Since all HTML paths are run through the webserver, they are all based on the Web layout (where the root = document root). When dealing with paths in PHP, you need to know how the files relate to each other in relation to the filesystem path structure. Since PHP is not limited by the webserver's document root, you can access files and directories outside of it. This is a common method of making a private directory to store things like uploaded documents or include files. Quote Link to comment Share on other sites More sharing options...
doubledee Posted November 26, 2011 Author Share Posted November 26, 2011 Web Root is not the same as the Filesystem root. The webroot is whatever the webserver is configured to use as their document root, eg $_SERVER['DOCUMENT_ROOT']. The Filesystem root is the root of the filesystem/drive (eg '/' on linux, C:\ (or relevant drive) on windows). Okay. Why is it that using a forward slash (/) works fine with HTML references, but not PHP? (I thought the logic was the same?) PHP paths are based on the Filesystem layout. Since all HTML paths are run through the webserver, they are all based on the Web layout (where the root = document root). When dealing with paths in PHP, you need to know how the files relate to each other in relation to the filesystem path structure. Since PHP is not limited by the webserver's document root, you can access files and directories outside of it. This is a common method of making a private directory to store things like uploaded documents or include files. Okay, but I posted my file structure above... 05_Debbie components body_footer.inc.php body_header.inc.php css _layout.css _main.css components.css top_menu.css index.php pages article_index.php howto_index.php So if I am in article_index.php, then this should correctly include my footer... <!-- BODY FOOTER --> <?php require_once('/components/body_footer.inc.php'); ?> (That would work in HTML because (/) would take you to the (web) root, and then you would drill down into "components" and so on...) Debbie P.S. I am using NetBeans and "05_Debbie" is my project folder, although I am using a "virtual host" for the HTML path stuff. Quote Link to comment Share on other sites More sharing options...
kicken Posted November 26, 2011 Share Posted November 26, 2011 So if I am in article_index.php, then this should correctly include my footer... <!-- BODY FOOTER --> <?php require_once('/components/body_footer.inc.php'); ?> No, it should not. (That would work in HTML because (/) would take you to the (web) root, and then you would drill down into "components" and so on...) Your not in HTML, your in PHP. PHP is based of the filesystem root, not the web root. Your file system root is going to be at least the directory containing 05_Debbie folder, maybe higher if there are more levels. Lets try a full example. I store all my websites that I am working on in my D:\ drive on my laptop in a folder called web. D:\ web\ siteA\ private\ public\ components\ header.php index.php siteB\ private\ public\ moreSites...\ Now, I setup my virtual hosts to use the public folder under each site as the document root. So for example: http://siteA/index.php would correspond do D:\Web\siteA\public\index.php In this setup, our roots would be: Web Root: D:\Web\SiteA\public Filesystem Root: D:\ Inside index.php, we have: <?php require('/components/header.php'); ?> Since PHP uses the filesystem root, that path is going to refer to D:\components\header.php Any HTML paths such as in an image tag are interpreted as a URL, which is always run through the server and is only capable of referencing something under the server's document root. This is why a / there references the web root because the / in HTML is just a shortcut for http://siteA/ Quote Link to comment Share on other sites More sharing options...
doubledee Posted November 26, 2011 Author Share Posted November 26, 2011 kicken, This summer I was using a config file that looked like this... <?php define('ENVIRONMENT', 'development'); //define('ENVIRONMENT', 'production'); // File Root define('ROOT', ENVIRONMENT === 'development' ? '/Users/user1/Documents/DEV/++htdocs/03_MySite/' : '/var/www/vhosts/MySite.com/httpdocs/'); // Web Server Root define('WEB_ROOT', ENVIRONMENT === 'development' ? 'http://local.dev3/' : 'http://www.MySite.com/'); // Secure Web Server Root define('SECURE_WEB_ROOT', ENVIRONMENT === 'development' ? 'http://local.dev3/' : 'https://www.MySite.com/'); ?> (Actually, the code above includes my "Virtual Host" reference, but is otherwise the same as before I was doing that.) The idea of this "config" file that was "pre-Virtual Host", it was a real pain in the ass to switch between NetBeans on my MacBook and my GoDaddy account because all of the paths were getting screwed up. Having one file where I could just comment/un-comment one line of code was a dream!! Then in August, someone - several people actually - encouraged me to create my first ever "Virtual Host" so I wouldn't need this "config" file... My "Virtual Host" seems to have eliminated the need for things like this... <div id="header"> <a href="<?php echo WEB_ROOT ?>index.php"> But apparently here there is still some confusion on my part with stuff like this... <!-- Include BODY HEADER --> <?php require_once(ROOT . 'components/body_header.inc.php'); ?> I guess I thought I could just do this... <!-- Include BODY HEADER --> <?php require_once(ROOT . '/components/body_header.inc.php'); ?> This seems to be okay... <!-- Include BODY HEADER --> <?php require_once('../components/body_header.inc.php'); ?> Hope you follow me? Debbie Quote Link to comment Share on other sites More sharing options...
Philip Posted November 26, 2011 Share Posted November 26, 2011 Don't get the web accessable root mixed up with the filesystem root. Just because you name a constant "root" doesn't mean it is the same thing as the root directory. In this case, with your constant ROOT the following are the same when in a development env: <?php // Defining your root define('ROOT', '/Users/user1/Documents/DEV/++htdocs/03_MySite/'); // These are the same: require ROOT . 'file.php'; // as this: require '/Users/user1/Documents/DEV/++htdocs/03_MySite/file.php'; // The following will not, because it will look for C:\file.php instead of C:\Users\user1\Documents\DEV\++htdocs\03_MySite\file.php require '/file.php'; Using your example: <?php require_once(ROOT . 'components/body_header.inc.php'); // Requests the file C:\file.php instead of C:\Users\user1\Documents\DEV\++htdocs\03_MySite\components\body_header.inc.php // whereas... require_once('/components/body_header.inc.php'); // Requests the file C:\components\body_header.inc.php See the difference? Now, the ../ means "go to parent directory", as mentioned before. Actually, you can just read all about it here. No need for me to retell what has been told Quote Link to comment Share on other sites More sharing options...
kicken Posted November 26, 2011 Share Posted November 26, 2011 Then in August, someone - several people actually - encouraged me to create my first ever "Virtual Host" so I wouldn't need this "config" file... Having a single config file to control site settings like that is still a nice thing to have. It is typically unnecessary for paths but convenient if you have other settings that may change (eg, email settings, DB settings, etc) My "Virtual Host" seems to have eliminated the need for things like this... ... But apparently here there is still some confusion on my part with stuff like this... ... You need to understand when your dealing with a Web path/URL and when your dealing with a filesystem path. In HTML you are always dealing with a URL path. Your link of /index.php is transformed into http://local.dev3/ (or http://www.MySite.com/ depending on how you've accessed the site). PHP generally always uses a Filesystem path unless otherwise noted in the manual. As such, '/' always references the filesystem root. Whatever you configure your virtual host to has absolutely zero effect on this, php is not restrained by it in any way. It's generally easier to just use relative paths, as they do not generally change unless you go through and restructure your site and move a bunch of files around. You can also auto-detect your root using php's magic constants. I do this generally in my global config file and use it to define a few constants to refer to other directories. Example: define('BASEDIR', dirname(dirname(dirname(__FILE__)))); define('PRIVATEDIR', BASEDIR.DIRECTORY_SEPARATOR.'private'); define('PUBLICDIR', BASEDIR.DIRECTORY_SEPARATOR.'public'); define('ERROR_LOG', PRIVATEDIR.DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'errors.log'); That defines BASEDIR to point to where my site is located on the filesystem. eg, given my layout in my previous post, D:\Web\SiteA. Then I define additional constants based on that to determine the location of other directories/files. My code can use these constants when necessary. If you do build your site to use all relative paths, the above is not really necessary. Your main confusion seems to stem from your lack of understanding of when your dealing with a URL vs a Path though. You need to recognize when your dealing with each, and that there is a difference between the two and what is considered to be their 'root'. Quote Link to comment Share on other sites More sharing options...
doubledee Posted November 26, 2011 Author Share Posted November 26, 2011 Don't get the web accessable root mixed up with the filesystem root. Just because you name a constant "root" doesn't mean it is the same thing as the root directory. In case you didn't know, I am on a Mac... I defined "Root" to be where my *files* are located for my website. Is that wrong in this context? Since I am on a Mac, there is no "C:", but in terms of my Operating System, I suppose "root" is "MacIntosh HD"??? Or is "root" possibly "/Users/user1/Documents/"??? Isn't the term "root" relative and not predetermined? (For instance, this isn't a System Admininstrator forum, so why can't my "File Root" for my PHP app be... define('ROOT', '/Users/user1/Documents/DEV/++htdocs/03_MySite/'); In this case, with your constant ROOT the following are the same when in a development env: <?php // Defining your root define('ROOT', '/Users/user1/Documents/DEV/++htdocs/03_MySite/'); // These are the same: require ROOT . 'file.php'; // as this: require '/Users/user1/Documents/DEV/++htdocs/03_MySite/file.php'; I agree. // The following will not, because it will look for C:\file.php instead of C:\Users\user1\Documents\DEV\++htdocs\03_MySite\file.php require '/file.php';[/code] Using your example: <?php require_once(ROOT . 'components/body_header.inc.php'); // Requests the file C:\file.php instead of C:\Users\user1\Documents\DEV\++htdocs\03_MySite\components\body_header.inc.php I think you mixed some things up there?! Can you please repeat that again? Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
kicken Posted November 26, 2011 Share Posted November 26, 2011 Since I am on a Mac, there is no "C:", but in terms of my Operating System, I suppose "root" is "MacIntosh HD"??? Or is "root" possibly "/Users/user1/Documents/"??? Root on your mac is the '/' directory. Perhaps that is labeled as 'Macintosh HD' in your file viewer. I have zero experience on mac's so I can't really say for sure how it's represented in the GUI. Isn't the term "root" relative and not predetermined? No, root is an absolute term, not relative. In windows, root is the drive letter, eg C:\. Since mac's and linux/unix filesystems do not have drives like windows, root is the '/' directory. In any case, root is always the very top level of the file tree. If you open a shell and run cd / it will take you to your root. so why can't my "File Root" for my PHP app be... You can always setup a constant/variable which points as a specific directory and consider that your app's "Root" but that does not have any effect on what the "/" prefix on a path means, and there is no way to change it. You have to prefix all your paths with the constant/variable you have setup for it to have any effect. Quote Link to comment Share on other sites More sharing options...
doubledee Posted November 26, 2011 Author Share Posted November 26, 2011 Since I am on a Mac, there is no "C:", but in terms of my Operating System, I suppose "root" is "MacIntosh HD"??? Or is "root" possibly "/Users/user1/Documents/"??? Root on your mac is the '/' directory. Perhaps that is labeled as 'Macintosh HD' in your file viewer. I have zero experience on mac's so I can't really say for sure how it's represented in the GUI. Okay. Isn't the term "root" relative and not predetermined? No, root is an absolute term, not relative. In windows, root is the drive letter, eg C:\. Since mac's and linux/unix filesystems do not have drives like windows, root is the '/' directory. In any case, root is always the very top level of the file tree. If you open a shell and run cd / it will take you to your root. Okay, my bad. so why can't my "File Root" for my PHP app be... You can always setup a constant/variable which points as a specific directory and consider that your app's "Root" but that does not have any effect on what the "/" prefix on a path means, and there is no way to change it. You have to prefix all your paths with the constant/variable you have setup for it to have any effect. So what would be a more proper name for... /Users/user1/Documents/DEV/++htdocs/03_MySite App_File_Root? App_Root? Other? Or can I just use "ROOT" or "FILE_ROOT" and it is understood what I mean? (Don't wanna go to hell because I am abusing a term too much?!) Debbie Quote Link to comment Share on other sites More sharing options...
xyph Posted November 26, 2011 Share Posted November 26, 2011 Will you remember what it means 6 months from now? If the answer is anything but 'yes,' then use a more descriptive naming scheme. Ideally, you're using an IDE that keeps track of the variables/constants you've defined and included, so being descriptive won't necessarily mean more work. Quote Link to comment Share on other sites More sharing options...
doubledee Posted November 26, 2011 Author Share Posted November 26, 2011 Will you remember what it means 6 months from now? If the answer is anything but 'yes,' then use a more descriptive naming scheme. Well, I liked "ROOT", but apparently I shouldn't be using that name... BTW, what *is* the name for this path... /var/www/vhosts/MyWebsite.com/httpdocs/ I know this is where my physical files exist. So what is it called? I think I usually call it the "Web Root"?! (The more I think about this, the more I am starting to see I never really thought about what I was calling the "Web Root"?! I think I just always knew when I meant "Where the physical files are at" versus "Where you start in the URL.") Ideally, you're using an IDE that keeps track of the variables/constants you've defined and included, so being descriptive won't necessarily mean more work. Well, my config file above spells things out pretty clearly. It just seems there is a debate about my naming conventions. Debbie 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.