aCarella
aCarella

Reputation: 2578

Using a function in another file to find a file's path/address in PHP

I have a PHP function in another file (with a bunch of other functions) that returns a file's address. Call the file commonFunctions.php. See below.

function filePath($fPath) {
    $cutPath = str_replace("/var/www","http://myinternalwebsite",$fPath);
    return $cutPath;
}

When I want another file's address, I just include my function file by using include $_SERVER['DOCUMENT_ROOT'] . "/commonFunctions.php and call filePath(dirname(__FILE__)). The result is something like http://myinternalwebsite.com/myScript.php.

Is there a way to place dirname(__FILE__) inside of my function so all I have to do is reference the function by typing filePath()? I tried doing something like the following

function filePath() {
    $cutPath = str_replace("/var/www","http://myinternalwebsite",dirname(__FILE__));
    return $cutPath;
}

but that obviously gives me the address of the commonFunctions.php file and not the file that I am calling the function in.

Thanks in advance.

-Anthony

Upvotes: 1

Views: 72

Answers (1)

sodawillow
sodawillow

Reputation: 13176

Why not use something like this ?

$_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"];

This outputs the complete URL, with the file, as :

if URL is http://website.com/section/index.php it returns website.com/section/index.php

if URL is http://website.com/section/ it returns website.com/section/index.php

I mean, as I understood it, you may not need a function.

Upvotes: 1

Related Questions