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
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
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"); ?>

Edited by dingdong1
Link to comment
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
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"); ?>

Edited by dingdong1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.