Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Web Programming
Reload this Page <?php include("/inc/foobar.php"); ?>
Reply
 
Thread Tools Display Modes
(#1 (permalink))
Old
Junior Member
kendfrey is on a distinguished road
 
Posts: 7
Join Date: May 2012
Default <?php include("/inc/foobar.php"); ?> - 05-16-2012, 01:17 AM

Code:
<?php include("/inc/foobar.php"); ?>
I am trying to include one php file in another, using the above code. When I view the website, I get an error message, and the file is not included. The rest of the page appears fine.

My directory structure is like this:
Code:
public_html
+inc
|+foobar.php
+foo
|+index.php
+index.php
I currently have a workaround by specifying "inc/foobar.php" in index.php and "../inc/foobar.php" in foo/index.php.

I would like to get it to work with "/inc/foobar/php", or any other method which is consistent across my site.

I don't remember the exact error message, but it was something like: "Could not find '/inc/foobar.php' with include_path 'something or other'".

Can someone explain how to include a php file from my inc directory?
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
nevermore93's Avatar
Senior Member
nevermore93 is on a distinguished road
 
Posts: 298
Join Date: Dec 2011
Location: USA
Default 05-16-2012, 03:26 AM

You will need to use the full path with your Home Root. Find it in the right side of your control panel.

Example:
Code:
<?php include('/home/aXXXXXXX/public_html/inc/foobar.php'); ?>
Cheers!!


Reply With Quote
(#3 (permalink))
Old
Leder678's Avatar
Senior Member
Leder678 is on a distinguished road
 
Posts: 1,618
Join Date: Jan 2009
Location: Norway
Send a message via MSN to Leder678
Default 05-16-2012, 06:12 AM

If the file is in some other dir, you should use (as you currently do)
require_once("../inc/phpfile.php") (or include/require/include_once)

if the php file is in a dir above the include file, simply do this:
require_once("inc/phpfile.php"); (no starting slash)


Follow me on twitter @Mortenrb

W3Fools - Read and learn

Please AT LEAST read the 10 bolded lines of the TOS at:
http://www.000webhost.com/includes/tos.php
Reply With Quote
(#4 (permalink))
Old
Junior Member
kendfrey is on a distinguished road
 
Posts: 7
Join Date: May 2012
Default 05-16-2012, 11:08 AM

Thanks, nevermore93, I never saw that. Will try it out when I have time.
Reply With Quote
(#5 (permalink))
Old
Mathias's Avatar
Member
Mathias is on a distinguished road
 
Posts: 63
Join Date: Feb 2012
Default 05-16-2012, 02:30 PM

Try this:
Code:
<?php include '/inc/foobar.php'; ?>
Reply With Quote
(#6 (permalink))
Old
Junior Member
kendfrey is on a distinguished road
 
Posts: 7
Join Date: May 2012
Default 05-16-2012, 07:53 PM

Thanks again nevermore93. Worked like a charm.

Here is my final solution:

At the top of every file, I put:

Code:
<?php $root="/home/a*******/public_html"; ?>
And then I could include files like so:

Code:
<?php include("$root/inc/foobar.php"); ?>
Reply With Quote
(#7 (permalink))
Old
nevermore93's Avatar
Senior Member
nevermore93 is on a distinguished road
 
Posts: 298
Join Date: Dec 2011
Location: USA
Default 05-16-2012, 08:15 PM

Getting closer

You will need to try this for your actual include:
Code:
<?php include($root . "/inc/foobar.php"); ?>


Reply With Quote
(#8 (permalink))
Old
Junior Member
kendfrey is on a distinguished road
 
Posts: 7
Join Date: May 2012
Default 05-16-2012, 08:20 PM

My code works too. PHP expands variable names in double-quoted strings.
Reply With Quote
(#9 (permalink))
Old
nevermore93's Avatar
Senior Member
nevermore93 is on a distinguished road
 
Posts: 298
Join Date: Dec 2011
Location: USA
Default 05-16-2012, 08:24 PM

Quote:
Originally Posted by kendfrey View Post
My code works too. PHP expands variable names in double-quoted strings.
Yup you are right. Nicely done. I was working in singles.



Reply With Quote
Reply

Tags
directory, include, include_path, php, root

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com