Liquid Fire Posted January 4, 2007 Share Posted January 4, 2007 what is the difference fromrequire("./global.php");andrequire("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 ). Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/ Share on other sites More sharing options...
chronister Posted January 4, 2007 Share Posted January 4, 2007 The difference in the 2 is ./ means root and the other is current directoryif you have a directory structure like this..-root---folder1----folder2--./global.php will look here.-root-global.php--folder1and that is no matter where your calling it from, ./ automatically looks in root for the file, whereasglobal.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. Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152639 Share on other sites More sharing options...
Liquid Fire Posted January 4, 2007 Author Share Posted January 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152647 Share on other sites More sharing options...
chronister Posted January 4, 2007 Share Posted January 4, 2007 ./ 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. Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152656 Share on other sites More sharing options...
Liquid Fire Posted January 4, 2007 Author Share Posted January 4, 2007 rootindex.php( calls the global.php )-forums-global.php( calls other files like core_class.php)--includes--core_class.phpNow each include starts with ./ like this:./global.php./includes/core_class.phpnow 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. Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152659 Share on other sites More sharing options...
chronister Posted January 4, 2007 Share Posted January 4, 2007 I assume the following...rootindex.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 includesTo 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 Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152670 Share on other sites More sharing options...
Jessica Posted January 4, 2007 Share Posted January 4, 2007 "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 :) Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152674 Share on other sites More sharing options...
MCP Posted January 4, 2007 Share Posted January 4, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152676 Share on other sites More sharing options...
chronister Posted January 4, 2007 Share Posted January 4, 2007 Thanks MCP,I always thought that ./ referred to root... Good to learn something I thought I knew. Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152678 Share on other sites More sharing options...
Liquid Fire Posted January 4, 2007 Author Share Posted January 4, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152679 Share on other sites More sharing options...
MCP Posted January 4, 2007 Share Posted January 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152680 Share on other sites More sharing options...
Liquid Fire Posted January 4, 2007 Author Share Posted January 4, 2007 ok in my root/index.php i call global.php(in root/forums) withrequire_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. Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152682 Share on other sites More sharing options...
PFMaBiSmAd Posted January 4, 2007 Share Posted January 4, 2007 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.phpUsing 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. Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152683 Share on other sites More sharing options...
Liquid Fire Posted January 4, 2007 Author Share Posted January 4, 2007 but if all the includes have "./" in the front, won't it just search the currently directory? Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152685 Share on other sites More sharing options...
Liquid Fire Posted January 4, 2007 Author Share Posted January 4, 2007 i also have this in my codeset_include_path(".;d:/root.;d:/root/forums.;d:/root/forums/includes");init.php should be called according to what you said because it is looked in d:/root/forums/includes Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152687 Share on other sites More sharing options...
PFMaBiSmAd Posted January 4, 2007 Share Posted January 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152703 Share on other sites More sharing options...
djazz Posted January 4, 2007 Share Posted January 4, 2007 A solution ive found is to add an extra file called root_path.php on every folder with the following codeat the root put<?$root_path=".";?>and at every other folder<?$root_path="..";?>Then every time you want to include something useinclude('root_path.php');include("$root_path/folder/anyfile"); Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152761 Share on other sites More sharing options...
Liquid Fire Posted January 4, 2007 Author Share Posted January 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32786-trouble-with-includes/#findComment-152866 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.