Log in

View Full Version : Help with relative paths in includes.



shachi
01-27-2007, 11:03 AM
Hello everyone,

I hope you've seen something like this in popular (PHP)frameworks or wikis or bulletin boards(like dokuwiki). I hope you've realised that these softwares let you change the names of their directory and the software still works(without any other efforts to modify the main script). They use something like a "dynamic" include that finds the needed file where ever it is. I was making a script where I needed a similar thing but unfortunately even with hours of googling, I couldn't find it. So I was wondering if anyone has any idea to achieve this?

Any help would be greatly appreciated. Thanks for your time and patience.:)

BLiZZaRD
01-27-2007, 05:01 PM
errr...



<?php include("path/to/file.php"); ?>


I understand you can also use require(); the same way.

This must NOT be what you mean... is it?

shachi
01-28-2007, 07:31 AM
To tell you the truth, it is but not completely(about 33&#37;).

Let me show you an example:

The problem with relative URLs in include is:



<?php
include("/path/to/file.php");
/*... bla bla bla ...*/
?>


now, the until the "file.php" is inside the "to" directory inside the "path" directory, the script will work but what if I wanted to change the name of the "path" directory to, say "relpath"(where rel means relative), now I need to modify the path in all the scripts. What I want is a dynamic way where I don't need to modify the code when I change the directory name(because when the size of the code is huge, it is almost impossible for any "normal" user to go back to the main source code and change each and every path).

I hope I was able to clear my point.

Thanks for your help though.:)

mburt
01-28-2007, 12:41 PM
$path = "path/to/";
include $path."filenamehere.php";
And keep reusing the variable in every instance in your code. So then if you have to change the folder location/name simply change the variable "path"

Titan85
01-28-2007, 10:22 PM
I don't know how to do this, but I believe that this tutorial is what you are looking for: http://www.pixel2life.com/publish/tutorials/192/relative_linking_with_output_buffering/ . Hope thats what you need ;)

shachi
01-30-2007, 05:12 PM
Titan85: Been there, done that. Thanks anyways.:)

boxxertrumps
01-30-2007, 05:30 PM
So Some Sort of Code that Searches the Directory for a folder that Contains a Folder which Contains A Specific File?

This Could be Accomplished Using A Multi Dimensional array Generated like this...

$dir = array (
"folderA" => array (index.php, include.php, pic2.bmp),
"folderX" => array (zero.bmp, header.jpeg, .htacsess),
index.php,
picture.jpeg
)
then search it for a certain folder/file
So $dir[folderA][1] would be returned as folderA/include.php

I have No Idea How This Could be accomplished, but i Do Know its possible.

Twey
01-30-2007, 05:34 PM
mburt is correct: this is the method I've seen in every major PHP application. The path is stored as a variable in a separate file as close to the root of the application as possible ("config.php" for example), then included into other files, from whence it is used as part of the include path for further files.

include("/path/to/file.php");That's an absolute path. :)

You should also look at the tool sed. It's capable of doing recursive search/replace operations.

BLiZZaRD
01-30-2007, 05:37 PM
Quick interjection...

I have been working on an SQL/PHP problem for a while, and this last post has given me a new idea...

can I ask what the "corrrect" syntax is for a single array?

Here is what I am looking at, please correct or confirm for me:



$name = array (name1, name2, name3, name4);

//later on in the source...

value = ?first_name : $name;



What I am trying to get around is the *:* means either/or, so by making the other option an array, I could have more choices added to the second half, yes?

mburt
01-30-2007, 05:44 PM
You didn't supply an initial argument?


value = ?first_name : $name;
Shouldn't it be:

value = argument?first_name : $name;
I'm confused on this issue...

BLiZZaRD
01-30-2007, 06:54 PM
Yes, and the actual code is quite long and confusing. I was just looking for the array set up correctly and then called correctly in that spot.

Twey
01-30-2007, 07:30 PM
I, too, fail to understand your question.

Do you mean to set up something like this:
(condition1 ? array(option1, option2) : array(option3, option4))[condition2 ? 0 : 1];... selecting between arrays of options, then further refining the selection with another ternary operator? This seems elegant, if inefficient.

BLiZZaRD
01-30-2007, 08:11 PM
Okay here is the section:



foreach (array_keys($context['smileys']) as $location)
$context['smileys'][$location] = array(
'id' => $location,
'title' => $location == 'postform' ? $txt['smileys_location_form'] : $txt['smileys_location_popup'],
'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $txt['smileys_location_popup_description'],
'last_row' => count($context['smileys'][$location]['rows']),
'rows' => array_values($context['smileys'][$location]['rows']),
);


What I have done so far is to make 9 other pop ups. so the 'title' and 'description' is currently set up as an either : or situation.

I wanted to make it either : or, or, or ...

Looking for different ways to do this without having to rewrite the entire coding for 12 different php files.

Looking at the post above, gave me the idea to have the "or" part be an array in itself. so it would be either : (array)

where array would = or, or, or...

Twey
01-30-2007, 08:21 PM
I wanted to make it either : or, or, or ...Something like:
(condition1 ?
either1 :
(condition2 ?
either2 :
(condition3 ?
or1
)
)
)?

BLiZZaRD
01-30-2007, 10:33 PM
Not quite. I apologize for being obtuse. I don't know how to explain it well I guess.

Here is another attempt.



'title' => $location == 'postform' ? $txt['smileys_location_form'] : $txt['smileys_location_popup'],
'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $txt['smileys_location_popup_description'],


What this script does, is shows a table. As it stands the table has 2 boxes inside it. The first is postform. the $txt part is the title of the postform's box. The other side of the : is the pop up box. (bt, the boxes contain a visual of what is contained in the actual post form and actual pop up).

I have made a total of 10 boxes. In this case, the one postform, and then 9 popups.

With the code as it is, the post form uses it's $txt as the title, but the other 9 'popup' boxes share the same $txt, so even though they are different they have the same title.

What I am attempting is to make it so it says something like:



'title' => $location == 'postform' ? $txt['smileys_location_form'] : $array,
'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $array2,


Where I would define $array and $array2 as something like:



$array = ($txt['smileys_location_popup'], $txt['smileys_location_popup_a'], $txt['smileys_location_popup_b'], $txt['smileys_location_popup_c'], ..etc),


In the hopes that the 9 pop up boxes in this table would then take on their own titles.

Does that make sense?

I know this is trial and error here, Which is why my original question was on the php code syntax, was it correct. Knowing if there were errors in the code would help when I get a parse error on the page. If I know the code is written correctly then I would know it wasn't that causing the errors. :D

Twey
01-30-2007, 11:41 PM
$i = 0;

'title' => $location == 'postform' ? $txt['smileys_location_form'] : $array[$i++],
'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $array2[$i++],Like that?

mburt
01-31-2007, 12:33 AM
'title' => $location == 'postform' ? $txt['smileys_location_form'] : $array[$i++],
'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $array2[$i++],
Shouldn't that comparison operator be === ? Comparing and assigning a value?

Twey
01-31-2007, 12:38 AM
Erm... no? === is for non-type-casting comparison. It could be used there, certainly, but no assignment is involved.

mburt
01-31-2007, 12:41 AM
Oops. Sorry, I thought it was something else. I guess I'll have to do my reading :rolleyes:

BLiZZaRD
01-31-2007, 02:16 PM
Cool. I will try that out and let you know what happens.

Thanks guys!

shachi
01-31-2007, 06:21 PM
Twey: So what are all those include(define(bla bla bla) bla bla bla) stuff?

Twey
01-31-2007, 07:03 PM
Hm? Where is define used?

shachi
01-31-2007, 07:52 PM
Sorry, my bad. I meant all those: include(dirname(bla bla bla) bla bla bla)