Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2005
    Posts
    72

    PHP include question

    Ok, this may be a stupid question, but how can I include a PHP file from a different directory?


    This is what I have tried, but it does not work:

    <?php include "../php/index.php"; ?>



    can anyone help me out? Thanks

  2. #2
    Join Date
    Nov 2003
    Posts
    477
    nothing special about this command
    http://www.php.net/include/

  3. #3
    Join Date
    May 2004
    Location
    NYC
    Posts
    793
    The syntax you posted is fine. So...

    Does the file exist? Is the path correct? Do you have read permissions on the file/directory? Is there a syntax error in index.php or one of the files that it includes?

  4. #4
    how far back are you going in directories? Make sure your directory structure is correct for your include statement...what you listed looks fine, depending on your structure, can you give a quick breakdown of your directory structure?

  5. #5
    The syntax is this:

    Code:
    <?php include "PATH/TO/FILE.PHP"; ?>
    For example, let's say you are in the root directory of your web folder, /public_html. Let's say you wanted to include a php file in your index.html file. Let's call this php file foo.php. Both files reside in the same directory.

    Your code should look like this:

    Code:
    <?php include "foo.php"; ?>
    Now, let's say that you want to add foo.php to your index.html file, but foo.php is in another directory, let's say /public_html/scripts. Your code would look like this:

    Code:
    <?php include "scripts/foo.php"; ?>
    The way you currently have your include set up is that it is trying to include the index.php and it is looking for it one directory below and then changing into the php directory.

    Hope this helps.

  6. #6
    Join Date
    May 2004
    Location
    NYC
    Posts
    793
    Quote Originally Posted by watchdoghosting
    The way you currently have your include set up is that it is trying to include the index.php and it is looking for it one directory below and then changing into the php directory.
    Eh??? here's his code:

    PHP Code:
    <?php include "../php/index.php"?>
    He's actually looking for it in a directory parallel to his own: one directory above (not below), and then down again, but ostensibly somewhere else.

    There's nothing wrong with the syntax, if that is indeed where the file is supposed to be.

  7. #7
    Thanks for clarification Sea Otter, I used the wrong terminology. Still a tad early for me, and I just had my Starbucks

    I never said the syntax was incorrect, I was just merely explaining it a little more. It just sounds that the file isn't where it should be.

    Thanks again for the clarification

  8. #8
    Join Date
    May 2004
    Location
    NYC
    Posts
    793
    Ah, no problem. I actually had just come back from getting coffee (and sitting outside on what will probably be the last sunny day here in NY) so I was hyper-jittery.

    And wanted to make sure the OP didn't get all twisted up. Speaking of which, where DID aristotl3 get off to???

Posting Permissions

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