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';
Related
My directory listing is: Users>Images>PreDefines
Where i am creating a php file i.e. index.php in PreDefines Folder. I want to include a php file from Users Folder i.e include('header.php');.
But There is a problem. header.php file contain include('title.php'); which not coming in PreDefines>index.php what should i do to include a file from another directory which includes others php files also.
you can simply navigate with ../ which means go backward one step and then go wherever you want
Assuming you are in Users/Images/Predefines/index.php and you want to include a file in Users you say include('../../header.php');
Or for example you have another folder in users called assets which has a file index.php.
You do: include('../../assets/index.php);
Always include in absolute to the directory you are working on.
Something that might help php has getcwd() which returns the current working directory.
Also if you are going to change the server such as from windows to linux or other type of servers instead of / use the DIRECTORY_SEPARATOR constant which will place a separator according to your server for example
include('..'.DIRECTORY_SEPARATOR.'title.php')
Hope i understood your question
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';
For requiring files in my PHP scripts I am using the following code :
require(dirname(__FILE__)."/../config.php");
with the config.php file being located a level higher than the file requiring it. However this appears to not work and the file cannot be located, with the following error :
failed to open stream: No such file or directory in /home3/**myusername**/public_html/PHP/access/login.php on line 3
I'm not sure what the error is as I have looked online and this appears to be the way others have done it. I believe however that the /.. is not causing it to go up a level.
EDIT 1
I changed the code to
require_once($_SERVER['DOCUMENT_ROOT'] . "/PHP/config.php");
and still receive the same error.
EDIT 2
The reason I am using an absolute path is because I have been led to believe this will work no matter where I call the file from, i.e if a file in a different directory includes this file (for this particular file it wont be the case but it will be in other files where I will use this) it will still include the config.php file correctly and not relative to the path of the file that included login.php.
EDIT 3
if I vardump the require path it prints the following :
string(52) "/home3/*myusername*/public_html/PHP/access/../config.php"
so obviously not going to the right location.
EDIT 4
Absolute path of file being required
/home3/*myusername*/public_html/PHP/config.php
Absolute path of file requiring it
/home3/*myusername*/public_html/PHP/access/login.php
try providing relative path to root. This is a better approach then providing relative to current file.
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
Remove the first / - "../config.php"
I have one root folder called GASS where I put all my php files and other related folders (templates,images,js,fonts,css)inside. When i try to run my project in localhost, http://localhost/GASS/alarm_A16GSM.php everything went smoothly. I wanted to change the URL to be more specific, http://localhost/GASS/alarmsystem/16zone/A16/overview.php thus i rename the php file and put it inside folders.
GASS
alarm-system
16-zone
A16
overview
However when i try to run the new URL,the page shows error.This is the error message:
Warning: include(templates/header.php): failed to open stream: No such file or directory in .....
Code for the first URL where the page load successfully.
<div class="overview"><a href="alarm_A16GSM.php" id="overview-selected"><span>
Code for the new URL where the page shows error.
<a href="alarm-system/16-zone/A16/overview.php" id="overview-selected">
It seems like i need to configure something which i do not know what it is.
How am i going to load the page successfully using the new URL? How am i going to traverse four levels up to the root directory so that the page load successfully? Why i cannot directly call the php file using the(alarm-system/16-zone/A16/overview.php) path?
p/s: sorry for my bad English.
It looks like there is a line in your Php file, probably like
include 'templates/header.php';
Include can't find the file using that relative path, because you moved the calling file.
Probably you could change that to
include '../../../../templates/header.php';
To get back down to the GASS folder that apparently has a folder called 'templates' with a file 'header.php' that is required.
An absolute path would be good, instead but it refers to the filesystem path, not webserver path - so you'd need to know your web root folder name on the server.
Copying all the folders (templates,images,js,fonts,css) to the folder overview will solve the issue. Now there is no template file on the folder 'overview' so header.php is failed to load. Another option is create a file save all the included file path and call this file.
I'm having a spot of bother with php includes. I have the following file structure
htdocs
index.php
login.php
php_includes
db_conx.php
check_user_status.php
within the db_conx.php file i've creates a variable $number = 10; for testing.
The db_conx file is included in the check_user_status.php file with the following code:
include_once("db_conx.php");
and that is working fine - i.e. i can echo $number and get 10.
However, I'm including the check_user_status.php file at the top of login.php with this code:
include_once("php_includes/db_conx.php");
and on this page I'm unable to echo out $number on this page (check_user_status.php).
I'm going to need this script included in many pages (since it checks whether the user is logged in or not). Am I doing something strange with the paths?
For relative paths you need to do this.
include_once("../php_includes/db_conx.php");
To break this down.
Your Current working directory is initially going to be htdocs/ if your hit that file in your browser.
the .. back you up one directory level (so the directory that contains both htdocs and php_includes)
then you want to follow down php_includes to get to db_conx.php.
This will become a problem when you do a file in a subdirectory. Assuming you and a page2.php to a htdocs/subpages/
Now if we follow those same steps we are not going to arrive at the same location.
A better approach is to get the path relative to an absolute location. I like using the document root (htdocs in your case), so:
include($_SERVER["DOCUMENT_ROOT"]."/../php_includes/db_conx.php");
will refer to the same place on the file system regardless of where it is used.
I think you can use __DIR__ magic constant
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(FILE). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)
This will help you with nested included files, infact the file path will be always set automatically and you don't have to deal with absolute paths.
If your file structure is correct, assuming that php_includes is NOT a directory within htdocs, you would need to do:
include_once("../php_includes/db_conx.php");