PHP include in Joomla! 1.7 - php

I need to include a file in one of my components in Joomla!. The file I want to include is located in the root of my Joomla! directory (where my index.php file is but my the file where the include is going is deeper in the administrator folder. It is in this file:
administrator/components/com_rsform/helpers/rsform.php
How can i put an include in that file that will link back to the file in my root of Joomla!? I need the path to be relative so that it is environment independent. Thanks in advance for your help!

Try the following:
<?php include_once (JPATH_ADMINISTRATOR .DS. 'components'.DS.'com_rsform'.DS.'helpers'.DS.'rsform.php'); ?>
There are also other variables that may help you:
JPATH_SITE = /your_local_path_to_site/ i.e: /var/www/mysite
JPATH_COMPONENT = /your_local_path_to_site/components/the_component_you_are_in i.e: /var/www/mysite/componentes/com_rsform
Hope that helps!

JPATH_SITE is constant always contains a path on the server's file system.
Please have a look onto this one.
http://www.latenight-coding.com/joomla-faqs/developers/get-path-joomla-folder.html

Related

Joomla current path

I have Joomla Component administrator/components/com_mycomp
And I have import.php file in this folder.
How I can access import.php from com_mycomp.php.
require_once('./import.php');
Gives error file not found. Because current path is /administrator , instead of administrator/components/com_mycomp.
The easiest way is to use PHP's __DIR__ constant, that always points to the current file's directory.
<?php
require_once __DIR__ . '/import.php';
use JPATH_BASE;
<?php
require_once(JPATH_BASE.'/directory/subdirectory/import.php');
?>
https://docs.joomla.org/How_to_find_your_absolute_path
Best way is to use Joomla constants, use
JPATH_ADMINISTRATOR - The path to the administrator folder.
or
JPATH_COMPONENT_ADMINISTRATOR - The path to the administration folder of the current component being executed.
for more refer-
https://docs.joomla.org/Constants
Let me know if still face any issue.

How to properly include or require a file in an another folder from a file in an another folder

For example, I am going to require index.php (file found in config folder) in index.php which is in the anotherfolder. I am just doing this require dirname(__dir__).'config/index.php'; what I understand in this line is that it will get my working directory(blog1) and find the folder named config and see if there's an index.php in it. This would get the job done but I'm not sure if I am doing it right, and is my understanding is correct?
for example this is my directory:
blog1
config
index.php
anotherfolder
index.php
If anyone can help me with my little problem that would be great, (don't mind the - sign I don't know how to else illustrate a directory.
If you are in anotherfolder and want to include index.php from config then use this code:
require __DIR__ . '/config/index.php';
P.S It is a good practice to define a common set of constants like BASE_URL and BASE_PATH in a configuration file and then include files with absolute addressing by using BASE_PATH e.g.
require BASE_PATH . '/file_name.php';
You should also make a habbit of that.

how to include from differing directory paths and depth

i am trying to include the same connect.php file as a part of the nav.php file throughout multiple different directories and directory depth's with a generic 'dynamic' substitute for the include path back to the connect.php file in the website root folder eg.
www.mywebsite.com/shop/shoes/sandals/index.php
and
www.mywebsite.com/shop/shoes/sandals/index.php
etc.
with the connect.php file being located in the root directory
www.mywebsite.com/connect.php
the code I am currently using in the nav.php (which is included in all pages and directories that are accessible by users)is as follows.
<?php
include '../../connect.php';
?>
however this will obviously not work in directory paths like
www.mywebsite.com/shop/shoes/sandals/index.php
I have no previous experience making a single connect.php file accessible by multiple directory paths using an absolute path with php.
however some guidance on this matter would be extremely helpful.
Thanks
You could try using something like
include $_SERVER['DOCUMENT_ROOT'] . "/connect.php";
To continue reading...
Hope this helps!

include file from different directory

following is my files directory structure...
Config
config.php (is calling some files too like includes/class.DB.php)
Now i have created another folder Admin and created new config.php (and calling root config file by require_once '../config/config.php';) its loading config file correctly but showing errors on includes/class.DB.php .
I hope, you have got my an idea of my problem, what is the way to achieve this, by USING DIR_NAME/ $SERVER['DOCUMENT_ROOT']
please help.
use
dirname(__FILE__); // or, if you have +5.3, use __DIR__ instead
so if you have this structure
includes
-class.php
admin
-admin.php
config
-config.php
So you can use
//admin.php
include(dirname(__FILE__)."/../config/config.php");
//config.php
include(dirname(__FILE__)."/../includes/class.php");
it always aims to the same directory!
Try:
include(dirname(__ FILE__)."/../include/connection.php");

How to include file from another directory

In the root (www) I have two folders.
In the first folder, "folder1", I put a file called register.php.
In the next folder, "folder2", I put files called header.php and footer.php.
I need to include the header and footer files from folder2 in the register.php file.
How can i do this? I tried to use this include ../folder2/header.php
..but it does not work
On some configurations, adding ./ (current dir) does the trick like this:
include './../folder2/header.php';
Alternatively, you can specify in terms of document root:
include $_SERVER['DOCUMENT_ROOT'] . 'folder2/header.php';
<?php include( $_SERVER['DOCUMENT_ROOT'] . 'folder2/header.php' ); ?>
include $_SERVER['DOCUMENT_ROOT'] . '/folder2/header.php';
would work from any directory of the site
it is called absolute path and it's the only reliable way to address a file
However, in real it should be something like
include $_SERVER['DOCUMENT_ROOT'] . '/cfg.php';
// some code
include $TPL_HEADER;
using a variable, previously defined in cfg.php
However, it may fail too. Because you can be just wrong about these paths
And here goes your main problem:
but it does not work
There is no such thing as "it does not work"
There is always a comprehensive error message that tells you what exactly doesn't work and what it does instead. You didn't read it yourself, and you didn't post it here to let us show you a correct path out of these error messages.
include files should generally be kept outside of the server root.
lets say your setup is;
www/website1
and
www/includes
Then you php.ini file, or .htaccess file should stipulate that
include_path=www/includes
then from any of your files, in any directory, no matter how far down the trees they go you simply do:
include 'myfile.php';
where myfile.php is at www/includes/myfile.php
Then you can stop worrying about these issues
include dirname(__FILE__).'/../folder2/header.php';
Try This it is work in my case
<?php require_once __DIR__."/../filename.php";?>
As the PHP manual states here $_SERVER['DOCUMENT_ROOT'] is "The document root directory under which the current script is executing, as defined in the server's configuration file." For this example, $_SERVER['DOCUMENT_ROOT'] will work just fine but. . . By using the new "magic constants" provided in >= PHP 5.3, we can make this code a little safer.
Put your includes in a subfolder, and use the magic constant DIR to make a reference to the included files. DIR returns the directory of the currently executing php file. By using this, you can move your folder containing all your includes anywhere you like in your directory structure, and not need to worry if your includes will still work.

Categories