This is an historic archive of the Grow Collective web site as it was in 2008.
Jon Tan, Jon Gibbins and Alan Colville have gone on to found Analog.
It has been pointed out, and quite rightly so, that the original routine would sit in an infinate loop if the file you are trying to include could not be found. To that end I have re-written the looping routine as follows:
function incfile($file,$d=""){
for($i = 0; $i < 5; $i++){
if(!is_file($d.$file)){
$d.="../";
}else{
include ($d.$file);
break;
}
}
}
This new routine will only loop for a given number of times and now supercedes the original routine (function) shown below. All ather code remains the same.
Don't you ever get fed up when writing a script or page (in PHP of course), and you have to constantly keep typing '../' however many times in order to get to the include file required ? I know I do, and it was during one of these tedious typing sessions that I came up with the following little function. I have used it several time and found it to be a godsend, so I thought I would share it with you all ('cos I'm nice like that!).
Here is the function code:
function incfile($file,$d=""){
while(!is_file($d.$file)){$d.="../";}
include ($d.$file);
}
and here is how to call the function:
incfile("somefile.php");
So, how does it work ? Well, basically you add the function code to the top of any PHP file you are working in, then if you call the function as
incfile("somefile.php");or
incfile("somedir/somefile.php");for specific directory location, then the routine will run a loop and check if the chosen file exists in the current location, if not, it will add
../
to the front of the file name and check again. When the file is found, it will break out of the loop and include the file in the page. How simple is that..?!
Of course it can be modified to the n'th degree in order to suit your own needs, but I found that works quite well as is.
So there you go. I hope some of you can make use of it. Please let me know how you get on, or if there are any improvements/modifications that you do, that you think would be useful to include.
Use RSS to be notified when we publish an article. What is RSS?
Comments:
Comments are turned off for this article.