douhan5547 2015-05-03 03:27
浏览 206
已采纳

如何在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?

  • 写回答

2条回答 默认 最新

  • du155305 2015-05-03 04:16
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建