Jump to content

[SOLVED] Simple question


ninedoors

Recommended Posts

I would like to use one config.php file for all my database connection on my site.  My question is, is there a simple syntax that I can use at the top of the pages I need, instead of including the whole path.

 

Right now I have my config.php file in a config folder so the path is config/config.php.

 

When I try to call it from lets say photos/insert/photos.php php tells me it can't find it.

 

The syntax I use is:

 

include 'config/config.php';

 

I know it's a noobie question but I would like to know because I have been having to put the config file in every folder which kinda negates the point.

Link to comment
https://forums.phpfreaks.com/topic/105930-solved-simple-question/
Share on other sites

I just tried that and I'm still getting this error:

 

Warning: include(/config/config.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\root\photos\insert\album.php on line 5

 

Here's the album.php file

 

<?
// Get values from form 
$nametemp=$_POST['album_name'];

include '/config/config.php';

$name = mysql_real_escape_string($nametemp);

$query = "INSERT INTO gallery_category (category_name) VALUES ('$name')";

mysql_query($query) or die("Query failed due to: ".mysql_error());
mysql_close();

header("Location: ../photos/uploadform.php");

?>

No, just put that in there. 

 

I tried putting all folders after the htdocs and failed still.

 

Warning: include(BigC/config/config.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\BigC\photos\insert\album.php on line 5

 

Nick

best idea is to define a constant from the root directory so you don't have to worry about a relative path.  You should do this for all your includes

example

<?php
define("includes_folder", $_SERVER['DOCUMENT_ROOT']."/configs/");
define("DB_CONFIG", includes_folder."db.php");
define("PHP_FUNC", includes_folder."functions.php");

#then to call them its simply
require_once(PHP_FUNC);
require_once(DB_CONFIG);
?>

for files that are configs or "include once" I like to use require_once because the files are both required and only needed once instead of an include function.

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.