View Full Version : Access file from any directory.
salman1karim
12-30-2013, 09:11 PM
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
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.
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
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.
salman1karim
12-31-2013, 05:36 AM
But where i have to define the
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).
But where i have to define the
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.
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.
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?)
salman1karim
12-31-2013, 08:00 AM
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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.