Slips Posted March 11, 2010 Share Posted March 11, 2010 Hi, Firstly thanks for reading this post, im a beginner at php, so this is a fairly simple problem I am trying to include a file with the default include path on a linux server. The include path is '.:/usr/lib/php:/usr/local/lib/php'. If my hosting account username was username4, all the subfolders in my hosting account are under username4. Ex: username4/public_html , username4/lib etc.. I dont have a very strong understanding of how linux directories work but from what i read up quickly over 3 hours, i decided to try and toss a file into the following folder and include it : username4/lib/php and it could not be found. I'm not sure whether i have to modify the include path or if i placed the file in the wrong directory. Help is greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/ Share on other sites More sharing options...
JonnoTheDev Posted March 11, 2010 Share Posted March 11, 2010 The include should have a path from the originating file location. i.e If my directory/file structure was: /docs/index.php /docs/includes/functions/common.php Within index.php if I wanted to include the common.php functions file I would simply use: <?php // index.php include('includes/functions/common.php'); ?> In your case public_html/ is the document root, so your index.php file sits in that directory. Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1024868 Share on other sites More sharing options...
Slips Posted March 11, 2010 Author Share Posted March 11, 2010 Yep i got that part, but i was wondering if i could use include('/fileToInclude.php'); - What im trying to achieve is for it to jump to the include folder specified in include_path. The reason i'm trying this is because i was trying to figure out a way to make the include folder invisible to the public (i.e not place it in public_html), and so that i dont need to use relative including because when i tried chain including (including dependent include files) i had some issues here and there. This method works fine in my offline localhost server because i can directly specify the include path to 'C:/wamp/www/includes' and the public_html folder is 'C:/wamp/www/public_html'. Maybe this method is not possible in the online server for some reason that you could hopefully explain to me ? Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1024872 Share on other sites More sharing options...
JonnoTheDev Posted March 11, 2010 Share Posted March 11, 2010 If you want to include a file from a directory outside of your document root i.e public_html/index.php includes/file.php public_ftp/ etc/ Then you have 2 options 1. Use the full path to the directory <?php // index.php include('/home/myfolder/www/includes/file.php'); ?> 2. Set the include path <?php // index.php set_include_path(get_include_path().PATH_SEPARATOR.'/home/myfolder/www/includes/'); include('file.php'); ?> http://uk2.php.net/set_include_path Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1024880 Share on other sites More sharing options...
XeNoMoRpH1030 Posted March 11, 2010 Share Posted March 11, 2010 I'd read up on the differences between relative and absolute paths. Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1024901 Share on other sites More sharing options...
Slips Posted March 20, 2010 Author Share Posted March 20, 2010 Hi, thanks for the replies, sorry about the late response. I tried setting the include path to /home/username/includes - which is outside the public_html folder. If there is a file includeFile1.php under includes, i am able to do include '/includeFile1.php'. No problems. Here is what i need to do - Include 'includeFile1.php' in 'publicFile.php'. But here is the problem - includeFile1.php has a dependent include in it to include includeFile2.php. I have a if/else logic in includeFile2 to assign a value to a variable $domainName depending on the $_SERVER['HTTP_HOST']. Basically i use this logic for the script to automatically decide where to pull static images/css/jss from, since the domain name varies on the offline local server and online server. The logic fails in includeFile2.php and assigns the value for the online server, when im using the offline server, BUT if i directly include includeFile2.php, in publicFile.php it works perfectly fine. Also any random variables assigned in includeFile2.php cannot be used in includeFile1.php, but if includeFile2.php is directly included in publicFile.php, it works great. My guess is that since includeFile2.php is outside the public_html folder, the php parser is having issues reading it if it is included in a file thats outside public_html? If there is no way to work around this im guessing ill just have to put all my include files in the public_html folder and prevent access to it by modying the .htaccess file? Thanks again for all the help everyone Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1029024 Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 You can includefile2 and 1 in livefile1? if you cant do that post includefile1 and includefile2 here and ill try com-binding them together so they are in the same file. Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1029025 Share on other sites More sharing options...
Slips Posted March 20, 2010 Author Share Posted March 20, 2010 You can includefile2 and 1 in livefile1? if you cant do that post includefile1 and includefile2 here and ill try com-binding them together so they are in the same file. Yup i can include includeFile1.php and includeFile2.php in publicFile.php (Note i just now edited my post and changed liveFile1.php to publicFile.php to avoid confusion). The thing is i dont want to combine includeFile1.php and includeFile2.php, because includeFile1.php contains all the universal server settings that many of my php include files will use, so its a common file used by many include files. I want to keep it in a separate file to make editing easier. Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1029029 Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 Then include both of the includes in your public file include('include1.php'); include('include2.php'); ? Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1029030 Share on other sites More sharing options...
Slips Posted March 20, 2010 Author Share Posted March 20, 2010 Then include both of the includes in your public file include('include1.php'); include('include2.php'); ? Yep, i could do that, but i want to build my application such that it includes files as and when necessary, this makes it easier to code, and easier to understand, apart from avoiding unnecessary includes. This will fix my problem but later if i come back to see why exactly publicFile.php needs includeFile2.php, it might confuse me.. I have faced this situation before thats why im reworking my entire php application , this is all part of it.. Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1029032 Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 write quotes in your code then helps you remember stuff # This is here to include functions, images and links etc Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1029034 Share on other sites More sharing options...
Slips Posted March 22, 2010 Author Share Posted March 22, 2010 Hey , i do that a lot , no problems, but i just wanted to use dependent includes to make it easier to modify and maintain code later. Anyways i figured out the issue. There happened to be another file called includeFile2.php in public_html folder and it was including that file, instead of the file i wanted it to include! I had a feeling it would turn out to be something like this lol, sorry for the troubles but thanks for the replies everyone! Quote Link to comment https://forums.phpfreaks.com/topic/194925-include-path-wont-work/#findComment-1029866 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.