Jump to content

Recommended Posts

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  :D

Link to comment
https://forums.phpfreaks.com/topic/194925-include-path-wont-work/
Share on other sites

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.

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 ?  :D

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

  • 2 weeks later...

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 :)

 

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.

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

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!

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.