Sunday, October 3, 2010

How to include an external file in Joomla?

Dear friends,

In some cases, you need to add an external file beside joomla file system in a module and component. In this case, your external file can't use the joomla libraries easily. Following is the way to use the external file.
Add following 10 lines of code on top of your external file.
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
/* If you need to use database object, add following line too */
$db=JFactory::getDBO(); // use this $db object for database related operation



 Now, use this file as normal joomla file. You can include the joomla libraries  files as per the requirement also.

Feel free to provide your comment.

11 comments:

  1. Very helpful post...

    ReplyDelete
  2. This is exactly what I needed. Thank you!

    Make sure that when using this code you are placing your files in the root of your Joomla! installation. Placing them elsewhere will throw an error.

    ReplyDelete
  3. By using this code, you can add an external file in your component and module also. The above code is not limited to a root file only. Maybe, you have to change in path.

    ReplyDelete
  4. Hi, I tried to use the code, but I got this error:
    Warning: require_once(/home/greenski/public_html/gsfiles/competencies/beh/includes/defines.php) [function.require-once]: failed to open stream: No such file or directory in /home/greenski/public_html/gsfiles/competencies/beh/page2.php on line 13

    ReplyDelete
    Replies
    1. Hello,
      To include an external file in joomla, you have to include defines.php and framework.php in the external file. Here, in the code, I am just including those files.
      You are adding page2.php as an external file then please modify this line:
      define('JPATH_BASE', dirname(__FILE__));
      So that value of JPATH_BASE should be path of Joomla root directory.

      It will work. If not, let me know.

      Delete
    2. Hi Abhishek I got a "No configuration file found and no installation code available. Exiting..." (it trying to read framework.php but it seems installation directory makes error)

      and I want to use the components database class, is it available with this piece of code.

      Delete
    3. Hi, You can use the Joomla database class and functions. For this, you can refer the code snippet of the post. As per the location of your external file, you have to change the below line of code so that value of JPATH_BASE should be path of Joomla root directory:

      define('JPATH_BASE', dirname(__FILE__));

      The above code is specific for Joomla 1.5 version.
      If your project is also in 1.5.x, You can share your code if you still face any issue.

      Delete
  5. "No configuration file found and no installation code available. Exiting..."
    I am getting This error...

    ReplyDelete
  6. Hi, i am using joomla 2.5 version. i try this code it didn't work. Any other modification i have to make

    ReplyDelete
  7. Thank you for this. I have been trying to get language translation working in an AJAX-enabled module, and this is precisely what I needed. Cheers!

    ReplyDelete

Thanks for your valuable comments.

Important queries based formulas to watch performance of a MySQL based application

(1) Session Vs global status variables value select G.variable_name,G.variable_value GlobalValue,S.variable_value SessionValue from (selec...