Include & __FILE__

Discussion in 'PHP' started by AiComPatim, Nov 19, 2006.

  1. #1
    If I use dirname(__FILE__) I know where my php file is.
    But if I include a file which contains this, I get the directory where the included php file is, but what I really want it is to know wich is the directory where the php file that contains the include is.

    I hope you understand my question and help me to find a solution.
     
    AiComPatim, Nov 19, 2006 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It might help if you tell us a little more about what you're trying to do... there may be an easier way.

    Of course, you could always just set a variable in the original file and then call that variable in the included file. For example, in the original file before the include, do:
    $including_directory_name = dirname(__FILE__);

    Then, in the included file, just reference $including_directory_name.

    But like I say, it might be better to explain the reason you're trying to do this to see if there's a better way.
     
    TwistMyArm, Nov 19, 2006 IP
  3. AiComPatim

    AiComPatim Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you twistMyArm
    This script shows a list of graphic files in the directory.
    I have several directories with graphic files.
    Instead of putting the same php script (ListFiles.php) in every directory, I pretend to put a php file with an include instruction

    calling for that php script that is in another fixed place.
    Unfortunately, in this case I get the same list of the graphic files that are there, not those that are in every directory.
    Do you understand my problem now?

    Thanks again


     
    AiComPatim, Nov 19, 2006 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ah yes, that makes sense, then.

    You're going to have to change the files that are including the main script, but essentially you can do what you want by doing something like the following. For your main script, use the code:

    
    <?
    function display_images_in_directory( $the_directory ) {
        $dir=opendir($the_directory);
        while ($file=readdir($dir)) {
            if (strstr(strtolower($file),'.jpg') || strstr(strtolower($file),'.png') || strstr(strtolower($file),'.gif')) {
                echo '<a onClick=window.open("'.$file.'","text","width=700,height=500,") href="javascript:;">'.$file.'</a><br >';
            }
        }
        closedir($dir);
    }
    ?>
    
    Code (markup):
    and then for each of your 'including' files, have the following code:
    
    <?php
    include "path/ListFiles.php";
    display_images_in_directory( dirname( __FILE__ ) );
    ?>
    
    Code (markup):
    That should work with a little tweaking.
     
    TwistMyArm, Nov 19, 2006 IP
  5. AiComPatim

    AiComPatim Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, I imagined it should be somethin like that.
    Thank you very much.

    AiComPatim
     
    AiComPatim, Nov 20, 2006 IP
  6. weknowtheworld

    weknowtheworld Guest

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    gr8 info on how to use directory... helped me.. thanks..
     
    weknowtheworld, Dec 20, 2006 IP