Results 1 to 1 of 1

Thread: how do I include a document from any file on my site?

  1. #1
    Join Date
    Jun 2011
    Posts
    1

    how do I include a document from any file on my site?

    I'm trying to use templates on my site that I include with the include_once() php statement. Unfortunately, I cannot figure out how to include files *globally*. This is my directory structure:

    Code:
    home
       user
          public_html
          css
             site.css
          files
             a_file.php
          templates
             main.php
          index.php
    I'm in a_file.php, and I'm trying to use the site.css style sheet (which doesn't use an include statement, bad example) as well as main.php. However, if I use include_once(/templates/main.php), the file can't be found. If I use include_once(/home/user/public_html/templates/main.php), the file can't be found. If I use include_once(../templates/main.php), it works, but here's the main question.

    What would I use to reference that style sheet and the php file throughout the entire site? Is there a single way to refer to it (something like /home/user/public_html/templates/main.php) that's the same no matter where I put it in public_html? It defeats the *entire* purpose of templates if I can't store them in a subfolder or have to add a new one every time I use it for a page in a different folder.

    Thank you for the help!

    EDIT: Someone referred me to the $_SERVER['DOCUMENT_ROOT'] value in PHP, but I'm still having major trouble getting my site to work. Here's the code in my header.php file, which is included on each page (I removed all meta tags to make it simpler).

    Code:
    <?php $doc_root = $_SERVER['DOCUMENT_ROOT']; ?>
    <head>
        <title>example.com</title>
        
        <link href="<?php echo $doc_root."/css/site.css"; ?>" rel="stylesheet" type="text/css"/>
    </head>
    In index.php, I have this code to include it:

    Code:
    <?php $doc_root = $_SERVER['DOCUMENT_ROOT']; ?>
    <?php include_once($doc_root."/templates/header.php"); ?>
    When I view the source code of index.php in the browser, the header is 100% there, and the <link/> line that points to the style sheet points to /home/user/public_html/css/site.css, which is correct. Unfortunately, the page isn't rendered using the style sheet, since the formatting is completely blank.

    Any ideas? I know I'm declaring $doc_root twice, but it doesn't seem to work if I don't include it in the header.php file as well.
    Last edited by pythonscript; 13 Jun 2011 at 08:51 PM.

Similar Threads

  1. PHP Include: If file not found, can i direct to a default file?
    By ukcoolat in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 04 Nov 2005, 09:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •