如何在JavaScript中从服务器链接获取文件的直接链接?

So, now I have a file. The file is at /home/johndoe/index.html.

I host, using something like XAMPP, which hosts the folder /home at localhost.

So this is the list of variables I have:

$server_addr = "localhost";
$server_root = "/home";
$file_dir = "/home/johndoe/index.html";

Now, in PHP, I'm trying to get the link to access /home/johndoe/index.html.

What I want to do:

function getLink($file_dir, $server_addr, $server_root) {
    // Do something here
    return "SOMETHING HERE";
}

echo getLink("/home/johndoe/index.html", "localhost", "/home");
// Expect 'localhost/johndoe/index.html' here.

I decided that only doing things on the thing is not a wise way.

Is there any native functions, or how can I write a function by 'using' the file instead of doing something on these three string's?

So you want to basically take $server_addr, remove $server_root from that and put localhost before it (I think, gathered from what I can decipher of your question XD)

I'd do this:

First of all, change $server_root from "/home" to "/home/"

Then:

//Variables to use
$server_addr = "localhost";
$server_root = "/home/";
$file_dir = "/home/johndoe/index.html";

//Function to get a click-able link
function getLink($file_dir, $server_addr, $server_root){
    
    //URL-ise? $server_addr
    $server_addr = 'http://' . $server_addr . '/';
    
    /**
     * Here we only want to replace the FIRST occurence of $server_root within $file_dir with $server_addr
     * */
    //Get the position of $server_root, in $file_dir, to make sure that it exists
    $root_pos = strpos($file_dir, $server_root);
    //Get the length of $server_root
    $root_len = strlen($server_root);
    
    //If it is not exactly (!==) equal to false (as 0 will be the case if it is at the very start)
    if($root_pos !== false){
        
        //Set the results by replacing only the first occurence
        $results = substr_replace($file_dir, $server_addr, $root_pos, $root_len);
        
        //Turn it in to a URL - Remove target="_blank" if you do not want it opening in a new tab
        $results = '<a href="' . $results . '" target="_blank">This is a link</a>';
        
        //Return the results
        return $results;
    }
}

echo getLink($file_dir, $server_addr, $server_root);

What this will do is:

Get the length of the "needle" and the position of the start of the "needle".

It will then replace everything from the start position, for the length of it, with the replacement.

This is a simple way of replacing only the first occurence of /home/, in case you have /home/ somewhere in the actual URL. I chose this method over preg_replace because you're using a variable that could contain anything, and preg_replace is a little heavy and complicated for something so simple.

I hope this works.

</div>
<a href="http://localhost/johndoe/index.html" target="_blank">Click Here</a>

JavaScript can't open a new tab unless it's in the context of a click event. If you would rather do that, then try:

document.getElementById('mybutton').addEventListener('click', function () {
  var a = document.createElement('a');
  a.href = "http://localhost/johndoe/index.html";
  a.target = "_blank";
  a.click();
});

Though in that context, window.open would do the same thing unless it was overwritten by an adblock plugin.