Jump to content

include function used from multiple folders


mweinberger

Recommended Posts

Hi,

 

I am using PHP (5) on my website and have a question with respect to using the incude set of functions. My folder layout is such that not every file is on the root. For instance,

 

/Folder1

    MyLonelyFile.php

/Folder2

    /FolderSub1

    /FolderSub2

/Folder3

    /FolderSub1

    /FolderSub2

          PublicFacingPage.php

/Scripts

    StandardHeader.php

 

If I have files in all the above folders for the sake of argument, how do I set the include statement, such that it can recognize a function in another PHP file. Say for example that a usr access PublicFacingPage.php. This file at the top would include the standard header, /Scripts/StandardHeader.php. Inside StandardHeader.php there is another include statement that I would like to add that would include in the file in the /Folder1/MyLonelyFile.php.

 

In my many attempts to solve this seemingly simple problem, I had my web hosting provider suspend my domain for "abusing" the PHP server. I read in PHP.net that I can use folder paths, i.e:

 

include( "http://www.mydomain.com/Folder1/MyLonelyFile.php" );

 

That did not make sense, as from my knowledge the include statement expects the server path and not the DNS path. As you might guess, that did not work. I tried using the absolute path, "E:\xxxxx....", but that did not work. (My provider uses Windows.) I tried a relative path fro the /Scripts folder, but that only works on some locations, not all. I tried changing the folder, then including the file, and changing the folder back, but that failed too. The only thing that I got for all my many attempts was frustration.

 

Thoughts?

 

Thanks in advance.

use relative paths from each file so in

PublicFacingPage.php

include "../../Scripts/StandardHeader.php";

and in StandardHeader.php

include "../Folder1/MyLonelyFile.php";

it doesn't matter that the script running is in

/Folder3/FolderSub2 all that matters is the path from StandardHeader.php to MyLonelyFile.php

 

Scott.

Hi Again,

 

Thanks for the reply. I tried this approach again, and no go. I decided to help myself and I put in require rather than include. Here is a real example.

 

Top page is /index.php.

 

index.php has:

 

<?php

// Include PHP scripts.

include_once( "./Scripts/Header.php" );

...

?>

 

/Scripts/Header.php has:

 

<?php

// Set the include paths.

// set_include_path('.\Scripts');

 

// Include PHP scripts.

require( "Captcha.php" );

...

?>

 

./Scripts/Captcha.php has:

 

<?php

require( "../captcha/captchac_lib.php" );

...

?>

 

It is at this point that I receive the following:

 

-----------------------------------------------------------------------

Warning: main(../captcha/captchac_lib.php) [function.main]: failed to open stream: No such file or directory in ...\Scripts\Captcha.php on line 2

 

Warning: main(../captcha/captchac_lib.php) [function.main]: failed to open stream: No such file or directory in ...\Scripts\Captcha.php on line 2

 

Warning: main(../captcha/captchac_lib.php) [function.main]: failed to open stream: No such file or directory in ...\Scripts\Captcha.php on line 2

 

Fatal error: main() [function.require]: Failed opening required '../captcha/captchac_lib.php' (include_path='.;c:\php4\pear') in ...\Scripts\Captcha.php on line 2

-----------------------------------------------------------------------

 

This file should be simple, because there are other files that call the header from other places, as I indicated above. :(

 

yes, this file exists. /captcha/captchac_lib.php, just for whatever reason the PHP environment does not see the file. My first thought is pathing, but maybe it is something else. The capitalization is fine, although it does not matter on a Windows server. Maybe something with include_once or something like that, but PHP complains right off the bat.

 

Thoughts and thank you in advance.

so your file layout is as follows

/index.php

/Scripts/Header.php

/Scripts/Captcha.php

/captcha/captchac_lib.php

?

 

then the includes should be like

index.php

<?php
require "Scripts/Header.php";
?>

Header.php

<?php
require "Captcha.php";
?>

Captcha.php

<?php
require "../captcha/captchac_lib.php";
?>

 

Scott.

 

 

 

 

Hi,

 

I tried it, but it does not work. Besides, if it did not work, then many other files that I would have included would not work. Something else is the problem. I just cannot figure it out.

 

Is it possible to use an absolute path? If so, how?

 

thanks

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.