Jump to content

Recommended Posts

what is the difference from

require("./global.php");
and
require("global.php");

the reason i ask is that the includes do not work if i i don't change the directory with chdir() to the location where the global.php in order to have the rest of the includes work even tho the location where the global.php is set in the php include path.  I want to be able to include this without using chdir() since i need to include this file in about all my files( 10+ so far ).
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/
Share on other sites

The difference in the 2 is ./ means root and the other is current directory

if you have a directory structure like this..

-root-
--folder1--
--folder2--

./global.php will look here.

-root-
global.php
--folder1

and that is no matter where your calling it from, ./ automatically looks in root for the file, whereas

global.php looks in the current directory. if you are calling from file.php, it looks inside folder1 for the file
-root-
--folder1--
  file.php
---global.php is expected to be here--
--folder2--

Hope this helps.
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152639
Share on other sites

thats is weird because the file is not located in the root directory( which i assume is where the root is ) but the root directory of my forums(ex. kaizendigital.com/forums ).  why is it looking the the root directory of the forums and not the site?  by the why the fourm systme i am using is Vbulletin and there forums are not helpful for this matter.
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152647
Share on other sites

./ will look in the root of the site. no matter what folder you are calling from, it will look in the root of the site.

where is the file in relation to where you want to call it?

includes are tricky sometimes, I found they don't like http:// paths as this could be a security risk. And it took me a while to figure out the whole ../../folder/file.php means 2 levels back then inside folder to target file, where ../file.php means 1 level back to target file.

give directory structure here and I can better help get this file included.

Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152656
Share on other sites

root
index.php( calls the global.php )
-forums
-global.php( calls other files like core_class.php)
--includes
--core_class.php

Now each include starts with ./ like this:

./global.php
./includes/core_class.php

now if i don't do:

chdir("d:\root\forums");

the includes with not work unless i get rid of the ./ part of the include however vbulletin is made of 30 of so file and changing there file could cause there system not to work.  i just want to be able to include these fileswhenever i need to without using the chdir, rather use php include paths.
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152659
Share on other sites

I assume the following...

root
index.php( calls the global.php )
-forums                                                      <- folder called forums
-global.php( calls other files like core_class.php) <- file is in forums
--includes                                                    <-another folder
--core_class.php                                          <- file inside includes


To call global.php from index.php, you simply need to call it like so...

[code]
include('forums/global.php');
[/code]

I use include instead of require, I am not sure of the differences between them, but I just like include because its easier to remember.

as a hypothetical for learning purposes, to call global.php from inside the includes folder you would do it like so.

include('../forums/global.php')

Hope this helps
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152670
Share on other sites

"require() and include()  are identical in every way except how they handle failure. include() produces a Warning while require() results in a  Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well."

I use require because I never include things which are optional :)
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152674
Share on other sites

Chronister is incorrect -- "./" refers the current directory, so the two following statements are pretty much equivalent (barring include path stuff)

require("globals.php");
require("./globals.php");


What you should do, is in globals.php you can use:
[code=php:0]
require(dirname(__FILE__)."\\anotherFileToInclude.php");
[/code]
which will then always work (assuming anotherFileToInclude.php is in the same folder as globals.php. Adjust accordingly.

What this does is __FILE__ contains the path of the calling file, in this case the full path to globals.php. Then, you get the directory name using the dirname() method on it, and finally you add on the filename, and you're done!
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152676
Share on other sites

the problem is that might work but only for that include.  each of these file include other files that include other files which means i am going to have to changed ALL the files require/require_once function calls which is soemhtig i am not willing to do because then if i update the forum system, i will have to do it again and so on, i am guessing using chdir is the only way to include it?
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152679
Share on other sites

No problem chronister! "/" refers to root and "../" refers to parent.

Liquid Fire, no, because __FILE__ depends on the *calling* file's location, so if you reference it in globals.php, then it will be globals.php path. So as long as your include files have the same relative path, then you will never have to change it.
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152680
Share on other sites

ok in my root/index.php i call global.php(in root/forums) with

require_once(global.php);

and this works fine because root/forums is one of the php include paths.  global.php calls init.php(root/forum/includes/init/php) with

require_once(dirname(__FILE__)."/includes/init.php");

but now i am getting an error becuase init.php is try to include a file using:

require_once("./includes/class_core.php");

I can i fix this so I don't have to change the vbulletin code( which is global.php, init.php, class_core.php) or so i only have to edit one of there files.
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152682
Share on other sites

You can set a search-able include_path (similar to the Dos/Windows PATH) using the set_include_path(...) function - http://www.php.net/manual/en/function.set-include-path.php

Using this you can set the include_path to be the current folder and any folders where you typically place your include files, then the include/require statements will search in these folders to find the include/require file.
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152683
Share on other sites

The include(...) section in the manual mentions a restriction -
[quote]If filename begins with ./ or ../, it is looked only in include_path relative to the current working directory[/quote]This is the problem you are currently having where you must change the working directory.

The key to using the include_path would be to just include using the bare file name and let it search the path. Give this a try with a couple of your files.

Also, MCP mentioned a leading / refering to the root (of your web site in the case of building URL's). I have not tried this, but doing - include("/includes/init.php"); (assuming that there is a folder "includes" in the root of your web site) should always reference the same file no matter where it is being included.
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152703
Share on other sites

A solution ive found is to add an extra file called root_path.php on every folder with the following code

at the root put
<?
$root_path=".";
?>

and at every other folder
<?
$root_path="..";
?>

Then every time you want to include something use

include('root_path.php');
include("$root_path/folder/anyfile");
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152761
Share on other sites

ok, well it looks like i have to change the current direct then.  the thing djazz says would be more work then doing chdir because i would need to change the way vbullletin includes all there files and every time i update there software i would need to do it again.  thanks for the help.
Link to comment
https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152866
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.