Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   php include help need (https://www.warriorforum.com/programming/996285-php-include-help-need.html)

Muhammad sakil 22nd September 2014 02:39 AM

php include help need
 
hey any one know ho to add many diffrent files from diffrent folders in a single php file..

this code works only for same folder where footer.php place

<?php include 'footer.php';?>

i want to add different files from diffrent folder like

<?php include 'head.php';?>

<?php include '/post/mypost.php';?>

<?php include '/comment/comment.php';?>

<?php include 'footer.php';?>

so please help me..

KirkMcD 22nd September 2014 10:30 AM

Re: php include help need
 
did you try it?
everything you typed could work.

kilgore 22nd September 2014 01:30 PM

Re: php include help need
 
Check your paths. When you put a "/" in your include statement you're saying to include a file relative to the root of the server, not the document root of your website.

Probably what you're looking for is something like:

PHP Code:

<?php include '../post/mypost.php';?>

(Note the ".." before the "/" above)

Alternatively you could do something like:
PHP Code:

<?php include '/path/to/DocumentRoot/post/mypost.php';?>


mralexanderca 24th September 2014 11:40 AM

Re: php include help need
 
What I would recommend is first setting up a file "paths.php" that will have few constants defined to the needed paths, for example:

// path to the root folder
define('DIR_ROOT',dirname(__FILE__) . '/');

// path to the config folder
define('DIR_CONFING', DIR_ROOT . 'config');

etc.

include DIR_ROOT . 'footer.php';
include DIR_ROOT . 'head.php';

You would then include it at the top of your script and include the files as needed, this is very prone to movement of the script and you will not have to change your constants often, even if you do, you would simply do so one.

Hope this helps.

Mr Alexander

Member8200 29th September 2014 09:41 PM

Re: php include help need
 
there's no issue on adding a single '/' before a folder name, and everything you type should work. please re-check your folder paths.

I also suggest you use require rather than include

require statement will produce a (E_COMPILE_ERROR) and stop the script
include statement will only produce a warning (E_WARNING) and the script will continue

'include' could give you more errors because the page will continue to execute even if the file you include is missing and that could affect the next line of codes


'require' will stop the page for further execution.


All times are GMT -6. The time now is 06:58 AM.