Jump to content

Recommended Posts

Hi, i need some help with directory in php, everything works fine when i use wamp with localhost, but when i ftp my files to a webhost i get this error:

 

Warning: include_once(/admin/root.php) [function.include-once]: failed to open stream: No such file or directory in /home/username/public_html/index.php on line 2

 

I think that I miss something in my config.php file, but Im not really sure how I can fix this since Im very new to PHP, I only need some basic PHP atm to make stuff easier for me.

 

admin/config.php

http://pastebin.com/hZWLJJ3D

 

index.php

http://pastebin.com/VFRKGNGX

 

Appreciate all the help i get!

Link to comment
https://forums.phpfreaks.com/topic/274836-php-directory-help/
Share on other sites

That leading slash means to look in the root of the drive. It's akin to saying C:\admin\root.php.

 

Naturally that's not where your file lives. It actually lives in /home/username/public_html/admin/root.php. So that's what you need to say.

Easiest method is

include_once($_SERVER["DOCUMENT_ROOT"] . "/admin/root.php");

Link to comment
https://forums.phpfreaks.com/topic/274836-php-directory-help/#findComment-1414293
Share on other sites

Warning: include_once() [function.include-once]: open_basedir restriction in effect. File(/usr/local/apache/htdocs/admin/config.php) is not within the allowed path(s): (/home/:/usr/lib/php:/tmp) in /home/username/public_html/index.php on line 1

 

line1

<?php include_once($_SERVER["DOCUMENT_ROOT"] . "/admin/config.php"); ?>

Link to comment
https://forums.phpfreaks.com/topic/274836-php-directory-help/#findComment-1414303
Share on other sites

Well that's certainly not normal.

 

Okay, DOCUMENT_ROOT won't work. Next best thing is a relative path. Assuming you have PHP 5.3 or later,

include_once(__DIR__ . "/admin/config.php");

 

Looks the same, huh? The DOCUMENT_ROOT version works by starting at the root of your website and going to the admin/config.php file from there. Using __DIR__ is relative to the file you put the code in, which could be the root or could be someplace else.

Say you have a file include/config.php and want to include the admin/config.php stuff. The two methods together then look like

// start at the website root, go to admin/config.php
include_once($_SERVER["DOCUMENT_ROOT"] . "/admin/config.php");

// start in this directory, go up a directory, then go to admin/config.php
include_once(__DIR__ . "/../admin/config.php");

Link to comment
https://forums.phpfreaks.com/topic/274836-php-directory-help/#findComment-1414307
Share on other sites

I tried again, but it just keep failing

 

DEMO - Here you can see the errors

 

index.php

<?php include_once(__DIR__ . "/include/header.php"); ?>
<?php include_once(__DIR__ . "/include/body.php"); ?>
<h1>THIS IS INDEX</h1>
<?php include_once(__DIR__ . "/include/footer.php"); ?>

 

admin/

config.php

<?php
$title = 'DomainName';
$url = 'http://localhost';
$header = 'include/header.php';
$body = 'include/body.php';
$footer = 'include/footer.php';
?>

 

include/

header.php

<?php include_once(__DIR__ . "/../admin/config.php"); ?>
<html>
<head>
<title><?php echo $title; ?></title>
<h1><a href="<?php echo $url; ?>">LOGO NAME</a></h1>

body.php

</head>
<body>
<nav>
<ul>
<li><a href="/music">Music</a></li>
</ul>
</nav>

footer.php

<footer>
copyright © 2013 - <?php echo $title; ?>
</footer>
</body>
</html>

 

music/

index.php

<?php include_once(__DIR__ . "/../include/header.php"); ?>
<?php include_once(__DIR__ . "/../include/body.php"); ?>
<h1>THIS IS MUSIC CONTENT</h1>
<?php include_once(__DIR__ . "/../include/footer.php"); ?>

Link to comment
https://forums.phpfreaks.com/topic/274836-php-directory-help/#findComment-1414324
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.