PDA

View Full Version : help with includes


ArthurR
06-19-2006, 04:49 AM
I want to Include a file that is in a subdirectory. The included file has Includes of its own.

Example
/Home
HomeFile.php (include /Test/Scripts/ScriptsFile.php)

/Home/Test/Scripts
ScriptsFile.php (include ../TestFile.php)

When I run this, ScriptsFile.php fails to find TestFile.php in the Test directory.

Is there a way to include a file in a subdirectory, and modify the path so that the referenced file's include statements will work, without turning relative paths into full paths?

Robert_J_Ellis
06-19-2006, 11:34 AM
<div class='quotetop'>QUOTE(ArthurR &#064; Jun 18 2006, 11&#58;49 PM) 6924</div>
I want to Include a file that is in a subdirectory. The included file has Includes of its own.

Example
/Home
HomeFile.php (include /Test/Scripts/ScriptsFile.php)

/Home/Test/Scripts
ScriptsFile.php (include ../TestFile.php)

When I run this, ScriptsFile.php fails to find TestFile.php in the Test directory.

Is there a way to include a file in a subdirectory, and modify the path so that the referenced file&#39;s include statements will work, without turning relative paths into full paths?
[/b]

<?php include(../../../ScriptsFile.php);?>

like that ? or am i mistaken ( i&#39;m tired >.< ).

ArthurR
06-19-2006, 12:15 PM
<div class='quotetop'>QUOTE(Robert_J_Ellis &#064; Jun 19 2006, 10&#58;34 AM) 6945</div>
<?php include(../../../ScriptsFile.php);?>

like that ? or am i mistaken ( i&#39;m tired >.< ).
[/b]


That does work, but the problem I&#39;m running into is that the file "../../../ScriptsFile.php" has relative path includes of its own, and searches for those includes from the relative position of the script that called it.

spib
06-19-2006, 12:57 PM
The trick here is to have your top level file control where it is in the hierarchy by declaring a variable which defines the path to the root of the application. For example

HomeFile.php
define&#40;&#39;IN_MYAPP&#39;, true&#41;;
&#036;rootpath = &#39;./&#39;
include&#40;&#036;rootpath . &#39;Test/Scripts/ScriptsFile.php&#39;&#41;;

ScriptsFile.php

if&#40;&#33;defined&#40;&#39;IN_MYAPP&#39;&#41;&#41;
**die&#40;&#39;Hacking attempt&#33;&#39;&#41;;

include&#40;&#036;rootpath . &#39;Test/TestFile.php&#39;&#41;;

The &#39;IN_MYAPP&#39; part makes sure that someone doesn&#39;t call ScriptsFile.php directly and pass in their own &#036;rootpath which would allow them to hack the server.

ArthurR
06-19-2006, 03:25 PM
Hmm, now I&#39;m getting an unexpected T_INCLUDE parse error.

My dev environment is Windows 2003, and I remember (vaguely) about include errors only on windows.

any clues?

spib
06-19-2006, 04:12 PM
There was a typo in my first section of code. Try this instead

define&#40;&#39;IN_MYAPP&#39;, true&#41;;
&#036;rootpath = &#39;./&#39;;
include&#40;&#036;rootpath . &#39;Test/Scripts/ScriptsFile.php&#39;&#41;;

ArthurR
06-19-2006, 09:22 PM
<div class='quotetop'>QUOTE(spib &#064; Jun 19 2006, 03&#58;12 PM) 6951</div>
There was a typo in my first section of code. Try this instead

define&#40;&#39;IN_MYAPP&#39;, true&#41;;
&#036;rootpath = &#39;./&#39;;
include&#40;&#036;rootpath . &#39;Test/Scripts/ScriptsFile.php&#39;&#41;;
[/b]

I&#39;m probably getting errors because the actual script I&#39;m calling is a pretty complicated beast in and of itself.

In any case, I am trying to do a proof of concept, that I can take HeavyEddie&#39;s Article Placer plugin and make it hold and execute php files stored as articles. My technique is probably very crude, but I&#39;m doing a right(4) on the files stored as articles, and if it finds ".php" it calls it with an include() statement instead of just displaying the file&#39;s content. I think this would be a really great way to get scripts to execute on a page, and not have to worry about including style sheets or opening them in other windows.

spib
06-19-2006, 09:34 PM
So how would that be different to a custom plugin?

ArthurR
06-19-2006, 10:21 PM
<div class='quotetop'>QUOTE(spib &#064; Jun 19 2006, 08&#58;34 PM) 6973</div>
So how would that be different to a custom plugin?
[/b]

Well, it would be a custom plugin. Since his plugin already accepts other files as articles, I wanted to see if it were possible to customize it even further to accept and process files which are php scripts. I&#39;m just see what I might be able to accomplish with the code, as I have some ideas in mind for maintenance scripts.