Include PHP file from parent sibling folder in word press - php

I try to include some PHP file to another one by using include function.
It works fine when files are at the same directory that simply can do
include (file.php)
and if a file is in the child folder like
include (folder/file.php).
but I want to make my root WordPress folder project cleaner then change the location of my template files to pages-template folder but unfortunately, I can't include files from another folder that are the sibling with pages-template.
I try this
include '../inc/package-save.php';
but I got errors
Warning: include(../inc/package-save.php): failed to open stream: No such file or directory
any trick to fix this.

Finally, I found the solution.
just put the dirname(__FILE__) function before the directory that I want to include.
This function will get the directory path that files that I write this function to it.
for example, if file be at C:\xampp\htdocs\amt-master\wp-content\themes\wp-bootstrap-child it returns this path
But as my case, I want to get the previous path to do that I should just put this function in the same function like this
include dirname(dirname(__FILE__))."/filename.php"
then it will return this path
C:\xampp\htdocs\amt-master\wp-content\themes
then you can write remaining path.

Try to use the following
include TEMPLATEPATH . '/inc/package-save.php';

Related

include file outside from public_html

Ι want to include a file outside from public_html in php.
I placed a file outside from public_html and tried to include this file, to a file that exists to public_html/myDirectory.
I tried this include '../../myFile.php'; but it's not working.
Where am i wrong ?
Thanks for any help.
Use full fledged path like.
include("/home/hostusername/myFile.php");
Replace hostname with your hostusername.
/ is absolute system path
to get your directory name use
echo $_SERVER['DOCUMENT_ROOT'];
where your file position the first folder use / only
include '/myfile.php';
The 2nd directory use file name
include '/filename/myfile.php';
other wise use curl or file get
file_get_contents("/myfile.php"); //http://yoururl.com/myfile.php
update your main code include alternate

Including a php file from a parent directory

I have a user_ip_location.php file in "/user/files/new" folder.
full url to the file is
/user/files/new/user_ip_location.php
I want to include this file to profile.php file that is located in Root dir
I have tried the following code
include("/././user_ip_location");
and
include("http://example.com/user/files/new/user_ip_location");
Both are not working, first is returning no such file or directory and the second is not loading the page and the browser is returning error could not locate to remote server
Is there any way to fix it?
You need to indicate previous directory by double dots ..
include("../../user_ip_location.php");
If I understand your question correctly you are in some directory and want to include a file from /user/files/new/user_ip_location.php. So just including it by specifying the path should do the trick.
In your example this would be:
include("/user/files/new/user_ip_location.php");
If the file you want to include is levels above your current directory just specify this by using .. like include("../../user_ip_location.php");
Try this:
include './files/new/user_ip_location.php';

Issues with defining a path for PHP include across multiple folders

cant figure out how to do what im trying.
I want a way to include PHP files so that i can call the same file from within any file in any level folder and it always points to the same files.
So i have a website setup e.g.
root
init.php
databaseconn.php
index.php
/css/
main.css
/settings/
user_profile.php
/template/
sidebar.php
/includes/
get_user.php
For example, on user_profile.php i am including the sidebar.php file.
For now i would use:
../template/sidebar.php
And then ../includes/get_user.php inside that sidebar file.
Which works fine. The problem however is that i also include the same sidebar.php file on my index.php page. This then causes the issues.
To counter this, i have tried this:
<?php
$isDevelopment = true;
$baseInclude = ($isDevelopment ? '//localhost/website/' : '//www.site.com/');
?>
And then when including any files across the site i have used:
include $baseInclude. 'template/sidebar.php';
When doing this however i am getting an error.
The error shows:
Warning: include(//localhost/website/template/sidebar.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/website/settings/user_profile.php on line 46
I dont understand why its looking for the file inside the user_profile.php file when it shows and i have defined that its coing from the localhost path...
Any ideas?
in your init.php file add this at the top
define('ROOT', __DIR__);
now in your other files use
include (ROOT . '/full/path/to/file.php');
in your code it would look like this
include (ROOT . '/template/sidebar.php');
One possible solution:
$baseInclude = getcwd();

require_once files from another directory requiring within

This is a big confusing but I'll try to be as clear as possible.
I am using the Starter Site PHP Template in Web Matrix for an assessmnent, now when I move several files such as the files containing the database details, header, footer ect.. into the admin file I am having an issue with my index.php and page.php.
These files use require_once() to include several files within the admin folder. However in the admin folder the files being required have require_once tags again within that directory.
When I use require_once('admin/database.php'); the database file has require_once(somefile.php) within it. There are numerous files like this being included.
How can I use include these files using require_once in the root directory without getting errors like these.
warrning: require_once(/Includes/simplecms-config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\starter-ste\admin\Includes\connectDB.php on line 2
The includes folder is located within the /admin/ folder.
php require statements will look for files in the current directory and directories in the php_include_path. You can solve your problem by adding your include directories to include path.
You can do this dynamically by calling set_include_path
$directories = array('/library', 'var/', get_include_path());
set_include_path(implode(PATH_SEPARATOR, $directories));
[EDIT]
Your server might not know where the root directory of the website is. So, first, get the document's root directory and append it to the file name like this:
$root = realpath($_SERVER['DOCUMENT_ROOT']);
require_once "$root/your_file_path_here";
Looks like you haven't enclosed the file name within quotes. So that
line should probably be like this:
require_once("/Includes/simplecms-config.php");

php include statement unable to open stream to file in directory above current directory

I am trying to include a file in a different directory using a relative file path however I keep getting the error:
failed to open stream: No such file or directory
The path of the file calling the include statement is
wp-content/plugins/php-code-widget/execphp.php
and the file I'm trying to include is
wp-content/uploads/espresso/templates/sidebar_widgets/register_widget.php
This is my include statement:
<?php include ('../../uploads/espresso/templates/sidebar_widgets/register_widget.php ?>
Anyone see the problem?
I would avoid the relative path.
So long as WordPress hasn't changed too much since I last used it, try something like...
include ABSPATH .
'wp-content/uploads/espresso/templates/sidebar_widgets/register_widget.php';

Categories