Home » Forum
NoNumber!

Joomla!® Websites & Extensions
development  -  support  -  consultancy

I try to respond within 48 hours (excluding weekends).
If I haven't responded by then, feel free to post a reminder or bug me via email.

My last reply was: 2 Hours, 7 Minutes ago.

Welcome, Guest
Please Login or Register.    Lost Password?

How to include a file
(1 viewing) (1) Guest
Go to bottomPage: 1
TOPIC: How to include a file
#5172
How to include a file 2 Years, 2 Months ago  
Including a php file
Example PHP file:
www.yourdomain.com/myfiles/file.php
{source}<?php
   require_once JPATH_SITE.'/myfiles/file.php';
?>{/source}

include/require vs require_once
You can also use 'include' (or 'require') instead of 'require_once', but only use that if you need the file included more than once on your page.
If your php file creates classes / functions, having it included more than once will cause errors like "Cannot redeclare class".

Setting variables for a php file
If you want to set variables that are used in the php file, you can simply set them before the require/include, like:
{source}<?php
   $name = 'Peter';
   $surname = 'van Westen';
   $interests = array( 'small fluffy things', 'green cantaloupe', 'toothpaste', '9V batteries' );
   require_once JPATH_SITE.'/myfiles/file.php';
?>{/source}


Including a text file
For text files, like .txt or .html you could use this syntax:
{source}<?php
   echo file_get_contents( JPATH_SITE.'/myfiles/file.txt' );
?>{/source}

NO html structures
Please keep in mind that files you include do NOT generate their own html structure (<html>, <head>, <body> tags).
If you include a file, it will be placed inside your Joomla content, which is already inside a full html structure.

So if you want to load in html files, make sure they only contain the text part (what is inside the <body>).
Otherwise you will either have to use iframes or use some more advanced php code to strip the html structure away.
Peter van Westen
Admin
Posts: 9091
User OfflineClick here to see the profile of this user
Last Edit: 2011/10/22 10:57 By peter.
The topic has been locked.
Need to contact me directly? Go to my contact page.
If you use any NoNumber! extensions, please post a rating and a review at the Joomla! Extensions Directory.
Are you happy with the support? Please consider buying a License Code to help me to continue development and support.
 
Go to topPage: 1
Moderators: Peter van Westen
hosting joomla
Open Source Training | Online Joomla! Training and Support
MaxCDN | Content Delivery Network | Accelerate your site to the max