include file outside from public_html - php

Ι 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

Related

How can i Include a php file in a directory which contain include('xyz.php'); from another directory?

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

Include PHP file from parent sibling folder in word press

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';

path issue with include in PHP

I have an index.php in root and a 'includes' folder containing header.php and footer.php.I included header and footer in index.php and it works fine but in header and footer I include some different files in config folder in root.
I know that I should use "../config/config.php" but it works with 'config/config.php'.
I included header and footer in index.php and I should use path according to the index that I included to ???
what if I include one file in several folders ???
Try to use __DIR__ it will give you the path compared to where your current file exist, also try to use include_once instead of include
DIR : 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.
Include your file like this from within your header.php and footer.php:
include_once DIR.'/../config/config.php';
do the same technique for other files
Yes your path should be according to index.php that's mean your root directory, not your includes folder.
At any time you can check such issues by
echo __DIR__;
to make sure from your path.

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';

understanding php directories

My database config class file is bellow
pard_config/class/Config.php
pard_config is a folder which is in the root folder.
And i have a json file which contain,host name,database,user and password details
pard_config/class/config.json
How would i include Config.json file to the Config.php file without reading it from the root.For now i'm doing it like bellow
Config.php
include("pard_config/class/config.json");
Config.json and the config.php files are in the same folder.So is there any way to read from that class folder like bellow ???
Config.php
include(config.json);
I would advice you to use a relative path and use the __DIR__ constant:
include(__DIR__ . '/config.json');
note that paths will be resolved from the script which where called in browser (or command line) .. like index.php. Therefore it will suite better in many cases to use a relative path.

Categories