Joomla current path - php

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.

Related

Isses with __DIR__ PHP code implementing the wrong home directory

My home directory path is /home9/ but when I using the __DIR__ code to get the path, I get /home/ it's missing the number 9, I know I can get the path by using $_SERVER['DOCUMENT_ROOT'], I just want to know why and how to fix it, I really need to use the __DIR__ code.
<?php
// this is the first code
echo __DIR__ .'<br>'; //I get /home/username/public_html/ is the wrog path
// this the secound code
echo $_SERVER['DOCUMENT_ROOT']; //I get /home9/username/public_html/ the correct path
?>
I contacted my hosting provider and they couldn't help, please help.
These are 2 different things.
Here in your case /home9/username/public_html/ is symlinked to /home/username/public_html/ where /home9/username/public_html/ is the actual document root. However __DIR__ returns the real path where symlink resolves to.
You can refer php documentation for more information in between these 2.

PHP require path - same directory

I have index.html in the root and all supporting files in /HTML. I have Google analytics code in a file in the HTML directory. It works from my index.html with this code in between the head tags...
<?php
require('HTML/GoogleAnalytics.html');
?>
but not in any of the supporting files in the HTML directory, same directory as the file i'm trying to require/include with this code...
<?php
require('GoogleAnalytics.html');
?>
from PHP.net "...include will finally check in the calling script's own directory and the current working directory before failing"
What am I doing wrong?
From PHP 5.3 (which is at this time after end of life cycle) and later you can use also __DIR__ constant , http://php.net/manual/en/language.constants.predefined.php
require(__DIR__ . '/GoogleAnalytics.html');
To make it relative to the current file, you can prepend dirname(__FILE__) like so:
require(dirname(__FILE__) . '/GoogleAnalytics.html');
By default, paths are relative to the file that the request originated from.
Try using this:
require('/HTML/GoogleAnalytics.html');

How to require a relative path in PHP

How can I PHP require a file using a relative path on a Linux server from inside a symbolic link?
I tried:
require_once(dirname(__DIR__) . "/app.config.php");
But that is throwing an error saying the file does not exist. Here is the file structure:
/srv/www/accounts/dev
app => /srv/www/myapp
index.php
app.config.php
So app is a symbolic link to /srv/www/myapp but when I try and require app.config.php php get's confused and tries to require /srv/www/myapp/app.config.php instead of /srv/www/accounts/dev/app.config.php.
How is this possible? Thanks.
Try require_once(__DIR__.'../app.config.php');
Hope that works :)
Just a note, if you're on PHP < 5.3, try dirname(__FILE__) instead of __DIR__ :)
require_once(__DIR__."/../app.config.php");
i think its the way the folders are setup, if u have like the /srv/www/accounts/dev/app.config.php. is in a different folder then /srv/www/myapp/app.config.php
ones in /accounts/dev and the other is in /myapp
you dont want a relative path you want absolute, relative would only be in that folder you have to at least let it know which folder to check, so i think you wanna go to the a different folder
You might try using realpath() to get the path. That is supposed to expand symbolic links and return the absolute path. Something like:
require_once(realpath('../') . "app.config.php");

PHP include in Joomla! 1.7

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

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");

Categories