Results 1 to 5 of 5

Thread: Access file from any directory.

  1. #1
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Access file from any directory.

    Hi,

    I want to access include file or image file from any directory in any page. my directory sturcture like

    Test
    -css (Test/css)
    -images (Test/images)
    -includes (Test/includes)
    -js (Test/js)
    -Folder1 (Test/Folder1)
    --Folder1_1 (Test/Folder1/Folder1_1)
    ---index.php ( Test/Folder1/Folder1_1/index.php)

    some of include files in include folder. if i used any file from include folder in index.php all images link and as well styling gone. If i use link like
    Code:
     include'../../includes/menu.php'
    than the menu will display but after that what i use style content in menu.php it will not work.
    I dont know how can i define root directory or server path in php and as well how to use it? This is simple thing but i confused.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by salman1karim View Post
    I dont know how can i define root directory or server path in php and as well how to use it?
    It's already defined: $_SERVER['DOCUMENT_ROOT']
    Depending on your server configuration, there may or may not be a trailing slash. In general, there is not; but you never know. You can do something like this:
    PHP Code:
    <?php
    define
    "ROOT",rtrim$_SERVER['DOCUMENT_ROOT'],'/' ) );
    include 
    ROOT."/includes/menu.php";
    which removes the slash if it exists. The constant also has the added benefit of being much easier to type.

  3. #3
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    But where i have to define the
    PHP Code:
    define"ROOT",rtrim$_SERVER['DOCUMENT_ROOT'],'/' ) ); 
    i have to create a separate config file or i have to define in every page. Because when i define the root path in this page (c:\inetpub\wwwroot\Test\Folder1\Folder1_1\main.php) after that its trying to fetch the file from the same folder like (c:\inetpub\wwwroot\Test\Folder1\Folder1_1\menu.php).

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by salman1karim View Post
    But where i have to define the
    PHP Code:
    define"ROOT",rtrim$_SERVER['DOCUMENT_ROOT'],'/' ) ); 
    As I said, the document root is already available in $_SERVER["DOCUMENT_ROOT"]. You can use it at any time.
    Defining a constant with the value is something you can do if you wish. I suggested it because many people find it convenient, but you don't have to.

    Quote Originally Posted by salman1karim View Post
    i have to create a separate config file or i have to define in every page.
    You can only define a constant once per script execution. If you try to define it again, you'll get an error.

    If you're talking about separate pages, then yes, a config file would be a good approach.

    Quote Originally Posted by salman1karim View Post
    Because when i define the root path in this page (c:\inetpub\wwwroot\Test\Folder1\Folder1_1\main.php) after that its trying to fetch the file from the same folder like (c:\inetpub\wwwroot\Test\Folder1\Folder1_1\menu.php).
    Yes, whatever you define will remain constant—that's what a constant is; a value that never changes throughout your script.

    (From reading your original post, I thought that's what you were trying to accomplish?)

  5. #5
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    yes i am trying to manage the code. we have 4 to 6 sub menus. i want to create separate folder for each menu.in that case all code will be manage. once i reached the goal; i shall let u know. thanks for your support.

Similar Threads

  1. Listing file in a directory problem
    By arsenalbates in forum PHP
    Replies: 4
    Last Post: 03-23-2011, 07:22 PM
  2. Restrict access to directory
    By Schmoopy in forum Other
    Replies: 7
    Last Post: 06-12-2009, 09:12 PM
  3. File access via php
    By neo_philiac in forum PHP
    Replies: 5
    Last Post: 11-12-2008, 03:02 PM
  4. .htaccess directory access
    By CoolD78 in forum JavaScript
    Replies: 0
    Last Post: 03-03-2008, 06:56 PM
  5. file and directory permissions
    By kosi in forum PHP
    Replies: 3
    Last Post: 05-30-2007, 09:59 AM

Bookmarks

Posting Permissions

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