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 @ Jun 18 2006, 11: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's include statements will work, without turning relative paths into full paths?
[/b]
<?php include(../../../ScriptsFile.php);?>
like that ? or am i mistaken ( i'm tired >.< ).
ArthurR
06-19-2006, 12:15 PM
<div class='quotetop'>QUOTE(Robert_J_Ellis @ Jun 19 2006, 10:34 AM) 6945</div>
<?php include(../../../ScriptsFile.php);?>
like that ? or am i mistaken ( i'm tired >.< ).
[/b]
That does work, but the problem I'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.
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('IN_MYAPP', true);
$rootpath = './'
include($rootpath . 'Test/Scripts/ScriptsFile.php');
ScriptsFile.php
if(!defined('IN_MYAPP'))
**die('Hacking attempt!');
include($rootpath . 'Test/TestFile.php');
The 'IN_MYAPP' part makes sure that someone doesn't call ScriptsFile.php directly and pass in their own $rootpath which would allow them to hack the server.
ArthurR
06-19-2006, 03:25 PM
Hmm, now I'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?
There was a typo in my first section of code. Try this instead
define('IN_MYAPP', true);
$rootpath = './';
include($rootpath . 'Test/Scripts/ScriptsFile.php');
ArthurR
06-19-2006, 09:22 PM
<div class='quotetop'>QUOTE(spib @ Jun 19 2006, 03:12 PM) 6951</div>
There was a typo in my first section of code. Try this instead
define('IN_MYAPP', true);
$rootpath = './';
include($rootpath . 'Test/Scripts/ScriptsFile.php');
[/b]
I'm probably getting errors because the actual script I'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's Article Placer plugin and make it hold and execute php files stored as articles. My technique is probably very crude, but I'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'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.
So how would that be different to a custom plugin?
ArthurR
06-19-2006, 10:21 PM
<div class='quotetop'>QUOTE(spib @ Jun 19 2006, 08: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'm just see what I might be able to accomplish with the code, as I have some ideas in mind for maintenance scripts.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.