11 Replies - 3904 Views - Last Post: 19 June 2010 - 12:59 AM Rate Topic: -----

#1 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 17-October 09

PHP include(root)

Posted 08 June 2010 - 08:00 AM

So here is what is in my index.php:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>
Homeworld Resurrection
</title>
<meta name="description" content="I'm trying to bring back the homeworld community">
<meta name="keywords" content="homeworld, homeworld2, homeworld 2, cataclysm, homeworld cataclysm">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="generator" content="Microsoft Notepad 6.1">

<link type="text/css" rel="stylesheet" href="/system/style_hw.css" />
<link rel="shortcut icon" href="/system/favicon.ico" />
<script type="text/javascript" src="/system/custom_form_elements.js"></script>
<?php include($_SERVER['DOCUMENT_ROOT']."generators.php");?>

</head>
<body><p align="center">
<table class="main">
<tr>
	<td valign="top">
	
	<?php maintable("nav",$_SERVER['DOCUMENT_ROOT']."navigation.php");?>
    
	</td>
	<td valign="top">
	<?php
		if (!isset($_GET['page'])) $page='news';
		else $page = $_GET['page']; 
		maintable("content",$page.".php");
	?>
	<br />
	<?php maintable("footer",$_SERVER['DOCUMENT_ROOT']."footer.php");?>
	</td>
</tr>
</table>
</p>
</body>
</html>



The thing I'm having issues with is the include($_SERVER['DOCUMENT_ROOT']."generators.php". It works fine on my computer, but when uploaded it gives me the errors below. I notice in the second error it gives me an include path that I think is what the $_SERVER['DOCUMENT_ROOT'] is returning, but I'm not sure. Is there a way to tell the include function to look directly at this generators.php file without hand-jamming it in to each individual sub-directory I put this index.php? I'm moving servers in a couple of months and I don't want to have to rewrite the inclusion paths. Three days of Google and I'm giving up.

Quote

Warning: include(/generators.php) [function.include]: failed to open stream: No such file or directory in /home/www/s4ndb0x.awardspace.info/index.php on line 15

Warning: include() [function.include]: Failed opening '/generators.php' for inclusion (include_path='.:/usr/local/php5/share/pear') in /home/www/s4ndb0x.awardspace.info/index.php on line 15

Fatal error: Call to undefined function maintable() in /home/www/s4ndb0x.awardspace.info/index.php on line 23


Is This A Good Question/Topic? 0
  • +

Replies To: PHP include(root)

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: PHP include(root)

Posted 08 June 2010 - 08:12 AM

DOCUMENT_ROOT comes from what is set in your web server's configuration. It should probably be set to the complete path to your files, i.e., in Apache:

<VirtualHost *:80>
DocumentRoot /var/www/mysite/htdocs


Was This Post Helpful? 0
  • +
  • -

#3 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: PHP include(root)

Posted 08 June 2010 - 08:12 AM

Your 2 servers are likely setting DOCUMENT_ROOT differently. Your live server seems to be setting this to '/' - the root directory. However, I guess you're wanting to get the current-working-directory. You can resolve to that path by using './generators.php'
Was This Post Helpful? 3
  • +
  • -

#4 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 17-October 09

Re: PHP include(root)

Posted 09 June 2010 - 01:01 AM

View PostJackOfAllTrades, on 08 June 2010 - 07:12 AM, said:

DOCUMENT_ROOT comes from what is set in your web server's configuration. It should probably be set to the complete path to your files, i.e., in Apache:

<VirtualHost *:80>
DocumentRoot /var/www/mysite/htdocs



My files are on a free web hosting service (awardspace.com) so I'm not sure how to change the settings on it. I'll do some digging to see if i can.

View Postmahcuz, on 08 June 2010 - 07:12 AM, said:

Your 2 servers are likely setting DOCUMENT_ROOT differently. Your live server seems to be setting this to '/' - the root directory. However, I guess you're wanting to get the current-working-directory. You can resolve to that path by using './generators.php'


This worked on both servers, but on the public server, if I go a level deeper in the directory tree it doesn't see it. The generators.php is at the very top level (of my stuff anyway) so all files have access to it if I tell it to go to the root directory of the website. I'm trying to get it to work on all levels so I don't have to type the "../" in the include for each level down I go in the directory. :(

Example 1 (top level, works): http://s4ndb0x.awardspace.info/
Example 2 (second level, doesn't work): http://s4ndb0x.award...info/tutorials/

This post has been edited by goku87: 09 June 2010 - 01:03 AM

Was This Post Helpful? 0
  • +
  • -

#5 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: PHP include(root)

Posted 09 June 2010 - 01:58 AM

Unfortunately, if you don't have the ability to modify the server configuration, then I can see no other way of doing this, other than entering the full path to the file relative to the CWD.

Is it really that hard to type '../'? If the answer is "yes," then you might instead copy the generators file to each directory it is required in, although this would be a hack.

Mark.
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4238
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: PHP include(root)

Posted 09 June 2010 - 03:51 AM

Assuming your server is running Apache, and you are allowed to use .htaccess files, you could use a .htaccess file to set the include path, which would allow you to include it without having to specify the path.

You would have to set the path manually to an absolute value, but you would only have to do it once and it would work on any PHP script in the directory or any of it's sub-directories.

Just create a .htaccess file with something like:
php_value include_path "/var/www/mysite/includes/"

And then just call the files in that directory by name alone, leaving out the path.
Was This Post Helpful? 1
  • +
  • -

#7 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 17-October 09

Re: PHP include(root)

Posted 14 June 2010 - 12:22 AM

After a few days of messing with it I've finally decided this was the best route to go. I couldn't quite exactly figure out how to change the root directory on the "real" server, and since it's a free server the tech support was lacking. So now the question is, is there a way to make this block of code smaller and/or cleaner, like using a for loop or something to put x amount of "../" in front of the *.php file? It's bulky, and I'm curious as to if it's possible for the file_exists() to tell when it's gone all the way to the root directory, whatever it's set at.

<?php
	if (file_exists("generators.php") == TRUE) {
		include("generators.php");
	} else if (file_exists("../generators.php") == TRUE) {
		include("../generators.php");
	} else if (file_exists("../../generators.php") == TRUE) {
		include("../../generators.php");
	} else if (file_exists("../../../generators.php") == TRUE) {
		include("../../../generators.php");
	} else if (file_exists("../../../../generators.php") == TRUE) {
		include("../../../../generators.php");
	} else if (file_exists("../../../../../generators.php") == TRUE) {
		include("../../../../../generators.php");
	} else {
		echo "FAILED TO INCLUDE GENERATORS.PHP";
	}
?>

This post has been edited by goku87: 14 June 2010 - 12:24 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: PHP include(root)

Posted 14 June 2010 - 12:51 AM

is the generators.php located in a specific place (of your project)? if so, you can define a base directory and call the generators.php relative to that.

like:
define("BASE_DIR", "/path/to/project/root/");

require BASE_DIR . "files/generators.php"; // errors out if file not found

This post has been edited by Dormilich: 14 June 2010 - 12:51 AM

Was This Post Helpful? 3
  • +
  • -

#9 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 17-October 09

Re: PHP include(root)

Posted 17 June 2010 - 06:58 PM

View PostDormilich, on 13 June 2010 - 11:51 PM, said:

is the generators.php located in a specific place (of your project)? if so, you can define a base directory and call the generators.php relative to that.

like:
define("BASE_DIR", "/path/to/project/root/");

require BASE_DIR . "files/generators.php"; // errors out if file not found


could you please elaborate on how the above code works? I'm not sure where to put it lol, and yes, the generators.php (and a few others) are in the "root" folder of the website, so if I were to link to them it would be "./generators.php".

This post has been edited by goku87: 17 June 2010 - 06:59 PM

Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: PHP include(root)

Posted 17 June 2010 - 10:06 PM

the first part is in a kind of configuration file you include on every page (you can tweak your php.ini (via .htaccess) to automaticly look for files in your config directory (so you can simply write require "config.php";). in this file you define the directory constant (part 1). later you can use this constant to add the relative part to any file inclusion, in your case require BASE_DIR . "config.php";.

(PS. I’ll post the code later, not much time now)

This post has been edited by Dormilich: 21 June 2010 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#11 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 17-October 09

Re: PHP include(root)

Posted 18 June 2010 - 07:54 PM

View PostDormilich, on 17 June 2010 - 09:06 PM, said:

the first part is in a kind of configuration file you include on every page (you can tweak your php.ini (via .htaccess) to automaticly look for files in your config directory (so you can simply write require "config.php";). in this file you define the directory constant (part 1). later you can use this constant to add the relative part to any file inclusing, in your case require BASE_DIR . "config.php";.

(PS. Iíll post the code later, not much time now)


okay, thank you. :) I'll anxiously await while I try to figure it out.
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: PHP include(root)

Posted 19 June 2010 - 12:59 AM

http://www.dreaminco...guration-files/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1