This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Wordpress root directory
I need to write a file in the root directory on a wordpress installation. This need to be in the same place as the wp-config.php file.
But i need a way to call the root url.
Is there a wp_root_dir() like function
Do you want root url or root dir?
Given that you're putting this file into the root of your wordpress I assume its not a plugin or something like that.
Here is how to get the root url
<?php echo get_bloginfo('url'); ?>
Otherwise if you want the path (to include() for instance) its perfectly reasonable to just do:
../../../file-in-root.php -> as many ../s as you need to go back directories
I do this all the time. If you think your directory structure may change a lot, you can just put the whole path to the file e.g.
/home/w/o/wordpress/web/public_html/file-in-root.php
If you're creating a plugin and you don't know the path to the file then obviously it would be different, but there are wordpress functions for that. However as you're using root I guess this is just for your own server/website configuration and this will be fine.
If you mean on the client side (http://www.myreallyawesomewebsitethatuseseordpress.com/blog/wordpress/), you can use get_bloginfo('url'). This will return the url which wordpress is installed.
If you mean on the server side, you can use the ABSPATH variable.
Usage:
Client side:
<?php
echo "You are using this site. Wordpress is on" . get_bloginfo('url');
?>
Server side:
<?php
echo "On the server, Wordpress is installed on" . ABSPATH;
?>
Related
This question already has answers here:
PHP absolute path to root
(8 answers)
Closed 2 years ago.
I'm using this code
$sort_data = get_sort('data/profiles/name.cms','¦');
The required "Data" folder is in the root of my host, but the php file that calling name.cms is in sub folder : styles/default/pages/left_member.php
and I'm failing to get that data from the name.cms file, any idea how to call the file from the root?
You could make use of the PHP $_SERVER functionality and target the root directory.
Example:
$sort_data = get_sort($_SERVER['DOCUMENT_ROOT'].'/data/profiles/name.cms','¦');
It's important to note that what's actually defined as the root directory is defined in your configuration file.
'DOCUMENT_ROOT' - "The document root directory under which the current
script is executing, as defined in the server's configuration file."
Source here.
UPDATE AS PER YOUR COMMENT:
Thank you for your help, I already tried it but it's getting whole
xampp directory, so it didn't work.
If you ever take your project "live", the $_SERVER['DOCUMENT_ROOT'] functionality should work. The reason why it doesn't work for you right now, is because you have a "project structure" in your root directory. I.e. localhost/myproject/index.php. What you meant by root is actually the project folder and not the actual root folder.
In that case, you can try 3 different options.
An absolute path without using the PHP reserved variable $_SERVER
to find it.
Example:
$sort_data = get_sort('/data/profiles/name.cms','¦');
Manually manipulate the directory path.
Example:
$sort_data = get_sort('../data/profiles/name.cms','¦');
You can add as many "level up", i.e. ../, as it takes to get to your desired starting point and then locate the folder.
Define your own "root" path variable.
Example:
$my_root = $_SERVER['DOCUMENT_ROOT'].'/myproject';
You can now use $my_root with your path, like so:
$sort_data = get_sort($my_root.'/data/profiles/name.cms','¦');
This question already has answers here:
What does the dot-slash do to PHP include calls?
(7 answers)
Closed 7 years ago.
What's the effective difference between:
include_once("../backend/example.php");
and
include_once("./backend/example.php");
My problem is that on my development-environment (XAMPP-Server) i had to use "./". But when i tried to upload my progress to the production-server, i had to change the paths to "../".
Thanks for your answer!
./ refers to the current working directory (It's basically redundant, but it reiterates that you're starting in the current directory, and not at the root / folder, or in PHP's case it may try other folders to find the file.). To see which directory that is, you can use the getcwd() function. ../ basically instructs php to go back to the parent folder and then into backend/example.php.
A few examples:
Let's say this is your root, and your cwd: /var/www/mywebsite/
../backend/example.php would refer to: /var/www/backend/example.php
./backend/example.php (And also just backend/example.php) would refer to: /var/www/mywebsite/backend/example.php
./= Actual folder
../= one folder back
you can define website path in config
like this
$path = 'http://yourwebsite.com'
Than you can do like this :)
include($path"/backend/example.php");
This question already has answers here:
Get relative path to a parent directory
(2 answers)
Closed 8 years ago.
So I have this sort of file set up. /cloud/ and /embed/ being two different subdomains.
www
-cloud
--config.php
--files
---formSubmit.php
-embed
--index.php
in /embed/index.php I have the following code:
include("/www/cloud/files/formSubmit.php");
in /cloud/files/formSubmit.php I have the following code:
include("../config.php");
If I am on cloud.website.com and I go to the formSubmit.php, everything works fine and the config file is included.
However, If I am on embed.website.com and I go to the index.php, I get an error saying that config.php was not found.
Does anyone know what do I need to do to include my formSubmit.php from either location and have my config.php included?
In this case, it seems your usage of relative paths is working and absolute paths are not. Whether that means the absolute path of /www/cloud/files/ is incorrect or not, I do not know. In my code, I tend to try to reference files relatively as much as possible like so:
// In embed/index.php
include_once dirname(dirname(__FILE__)) . '/cloud/files/formSubmit.php';
What that does is get the directory of the currently executing file and then it's parent directory, which would be www, and then goes back down the path from there to the file I need.
Subdomains should not make a difference when accessing files server side (as long as the files are hosted on the same server).
This question already has an answer here:
What is the best way to execute the same php script on different server?
(1 answer)
Closed 9 years ago.
Let's say I have 2 domain names 123.com and abc.com all on the same 1and1 server.
123.com is in a folder called 123 and abc.com is in a folder named abc.
How would I include a file stored in 123 into a page on the abc.com site.
I used to do this with PHP
<?php
$code = file_get_contents("http://www.123.com/file.html");
eval('?>' . $code);
?>
but it was stopped with PHP5 to prevent abuse.
Basically, I want to be able to edit one file of html and have it change on multiple sites.
I thought that
<?php include($_SERVER["DOCUMENT_ROOT"]/123/file.html); ?>
would do it, but it's not going to my server root, just the domain's root.
Is it possible to use php to get info from sibling folders on a server?
Thanks
Richard
Yes, you want the require() or include() function (or require_once()). See http://nl3.php.net/require .
This is also much faster btw, because there is no http connection being setup, but only the internal file system is being queried.
-- EDIT
oh wait, I see you've already found the include function. I think you should use either absolute paths, or use the '..' in the path:
include('/full/path/to/the/directory/file.html');
include($_SERVER['DOCUMENT_ROOT'] .'../123/file.html');
I don't know 1and1 and what they allow you to do... What I would suggest is have a 3rd folder at the same place as your dmain folders. Call it common. Then in each of your domains folders, where it better suits your needs, create a directory symlink to the common folder we just created...
Basically, it will do this: (absolute blind approximation of actual server)
1and1/your_account/123/common -> 1and1/your_account/common <- 1and1/your_account/abc/common
Also you can use symlink so you can open file from both directories.
Most of my website is in my root directory. And In that directory there is "css", "functions", "images" folder. Everything works fine when I include php files within index.php or any other root file. It includes it fine and executes it fine.
But problem occurres when I made folder "blog". So this is totally new and separate root folder with CMS and its own "root" files. And I try to include css from main root directory or some php files from "functions" folder in main root directory, Everything breaks down. I know I have to include it as ../functions/myfile.com. But this files includes some other files so it just wont work properly and won't be able to include other files properly.
Is there any idea how to fix this problem?
You can get to the root from within each site using $_SERVER['DOCUMENT_ROOT']. For testing ONLY you can echo out the path to make sure it's working, if you do it the right way. You NEVER want to show the local server paths for things like includes and requires.
Site 1
echo $_SERVER['DOCUMENT_ROOT']; //should be '/main_web_folder/';
Includes under site one would be at:
echo $_SERVER['DOCUMENT_ROOT'].'/includes/'; // should be '/main_web_folder/includes/';
Site 2
echo $_SERVER['DOCUMENT_ROOT']; //should be '/main_web_folder/blog/';
The actual code to access includes from site1 inside of site2 you would say:
include($_SERVER['DOCUMENT_ROOT'].'/../includes/file_from_site_1.php');
It will only use the relative path of the file executing the query if you try to access it by excluding the document root and the root slash:
//(not as fool-proof or non-platform specific)
include('../includes/file_from_site_1.php');
Included paths have no place in code on the front end (live) of the site anywhere, and should be secured and used in production environments only.
Additionally for URLs on the site itself you can make them relative to the domain. Browsers will automatically fill in the rest because they know which page they are looking at. So instead of:
<a href='http://www.__domain__name__here__.com/contact/'>Contact</a>
You should use:
<a href='/contact/'>Contact</a>
For good SEO you'll want to make sure that the URLs for the blog do not exist in the other domain, otherwise it may be marked as a duplicate site. With that being said you might also want to add a line to your robots.txt file for ONLY site1:
User-agent: *
Disallow: /blog/
Other possibilities:
Look up your IP address and include this snippet of code:
function is_dev(){
//use the external IP from Google.
//If you're hosting locally it's 127.0.01 unless you've changed it.
$ip_address='xxx.xxx.xxx.xxx';
if ($_SERVER['REMOTE_ADDR']==$ip_address){
return true;
} else {
return false;
}
}
if(is_dev()){
echo $_SERVER['DOCUMENT_ROOT'];
}
Remember if your ISP changes your IP, as in you have a DCHP Dynamic IP, you'll need to change the IP in that file to see the results. I would put that file in an include, then require it on pages for debugging.
If you're okay with modern methods like using the browser console log you could do this instead and view it in the browser's debugging interface:
if(is_dev()){
echo "<script>".PHP_EOL;
echo "console.log('".$_SERVER['DOCUMENT_ROOT']."');".PHP_EOL;
echo "</script>".PHP_EOL;
}
If I understand you correctly, You have two folders, one houses your php script that you want to include into a file that is in another folder?
If this is the case, you just have to follow the trail the right way.
Let's assume your folders are set up like this:
root
includes
php_scripts
script.php
blog
content
index.php
If this is the proposed folder structure, and you are trying to include the "Script.php" file into your "index.php" folder, you need to include it this way:
include("../../../includes/php_scripts/script.php");
The way I do it is visual. I put my mouse pointer on the index.php (looking at the file structure), then every time I go UP a folder, I type another "../" Then you have to make sure you go UP the folder structure ABOVE the folders that you want to start going DOWN into. After that, it's just normal folder hierarchy.
i had the same issue and found a code on https://css-tricks.com/php-include-from-root/ that fixed it
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/common/header.php";
include_once($path);
?>
None of the above answers fixed this issue for me.
I did it as following (Laravel with Ubuntu server):
<?php
$footerFile = '/var/www/website/main/resources/views/emails/elements/emailfooter.blade.php';
include($footerFile);
?>
Try to never use relative paths. Use a generic include where you assign the DocumentRoot server variable to a global variable, and construct absolute paths from there. Alternatively, for larger projects, consider implementing a PSR-0 SPL autoloader.