/    Sign up×
Community /Pin to ProfileBookmark

Script: MakeRelativePath(File1, File2)

Hi everyone! Im new here and I have been stumbeling on a quite beginners problem I think.
Im creating a CMS (content management system) and to make it easy for the end user I would like a function that returns a relative path from ex. “k:wwwnfreakpagespage.php” to the file “k:wwwnfreakincludeimageslogo.png”.

The Win-Paths is because I’m using WOSX.

Here you have the recent function code:

[code=php]function MakeRelativePath($Me, $Target)
{
global $INI, $thisFile;
$Me = str_replace(“\”, “/”, $Me);
$Target = str_replace(“\”, “/”, $Target);

echo $Me . “<br>n”;
echo $Target . “<br>n”;
// K:/www/nfreak/index.php
// K:/www/nfreak/include/image/logo.png

$MePath = dirname($Me);
$TargetPath = dirname($Target);
echo $MePath . “<br>n”;
echo $TargetPath . “<br>n”;
// K:/www/nfreak
// K:/www/nfreak/include/image

$MeDir = explode(“/”, $MePath);
$TrgtDir = explode(“/”, $TargetPath);
}[/code]

For example, this call (where __FILE__ = “K:wwwnfreakpagestestpage.php”)

[code=php]$Class->MakeRelativePath(__FILE__, “K:wwwnfreakincludeimagelogo.png”);[/code]

Should result in: “....includeimagelogo.png”

I need ideas of how I could make it do what I want it to.
Your name will be in the credits for this function If you want.

[SIZE=”1″][I]For those who dont know [B]WOS/WOSX [/B](Webserver-On-Stick) its a portable webserver (apache2, mysql5, php5 is the “standard” package). I highly recommend it if you need to be able to work on the pages at any time. then just upload them to the server when you have access to internet. Its found at [url]http://wos.chsoftware.net/[/url][/I][/SIZE]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@scragarFeb 12.2008 — [code=php]function relativepath($from, $to){
$retMe = '';
$from = dirname($from);
$to = dirname($to);
$f_ar = explode('/', $from);
$t_ar = explode('/', $to);
for($i = 0;
($i < count($f_ar)) && ($i < count($t_ar)) && ($f_ar[$i] == $t_ar[$i]);
$i++){};//find closest match
for($j = 0; $j < count($f_ar)-$i; $j++)// point back.
$retMe .= "../";
for($j = $i; $j < count($t_ar); $j++)// point forwards
$retMe .= $t_ar[$j]."/";
return ($retMe=='')?'./':$retMe;//if empty return current dir.
};


echo relativepath("k:/www/freak/pages/page.php", "k:/www/nfreak/include/images/logo.png");// "../../nfreak/include/images/"

[/code]
you'll have to do your own converting to /, but that's not too hard.
Copy linkTweet thisAlerts:
@TrualgauthorFeb 14.2008 — Thanks. I know how to do that so its a piece of cake now ?

Want to be in the credits?
Copy linkTweet thisAlerts:
@scragarFeb 14.2008 — I don't care if I'm mentioned or not. ?

do whatever makes you happy.
×

Success!

Help @Trualg spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...