简体   繁体   English

使用PHP将链接的动态href分配给另一个链接

[英]assigning dynamic href of a link to another link with PHP

I have this problem. 我有这个问题。 On my website I want to show the content of a folder with links. 在我的网站上,我想显示带有链接的文件夹的内容。 And if I click this link i want it to assign the source of the file to another link/button. 如果我单击此链接,我希望它将文件的源分配给另一个链接/按钮。 So if I click the download button it downloads the file I clicked on. 因此,如果我单击下载按钮,它将下载我单击的文件。

this is what i got to show the files as links: 这就是我要显示的文件作为链接:

PHP 的PHP

<?php
$path = "./files";

$dir_handle = @opendir($path) or die("Unable to open $path");

while ($file = readdir($dir_handle)) {
  if($file == "." || $file == ".." || $file == "index.php" )
  continue;
    echo "<a href=\"".$path."/".$file."\">$file</a><br />";
}

closedir($dir_handle);
?>

so this makes a link of every file in the folder but I cant figure out how to assign the source of the file I click on to a download button/link. 因此,这将链接该文件夹中的每个文件,但是我无法弄清楚如何将我单击的文件的源分配给下载按钮/链接。 Atleast I don't know a way that doesn't make a download link for every file. 至少我不知道不会为每个文件建立下载链接的方法。

Try this and note that this may not work depending on server and php settings. 尝试此操作,请注意,根据服务器和php设置,这可能无法正常工作。 I am assuming you are using properly configured Apache (or Apache-comparable) server: 我假设您使用的是正确配置的Apache(或与Apache可比)的服务器:

<?php

// Get the document root the server or virtual host is pointing to
$docroot = $_SERVER['DOCUMENT_ROOT'];
$docrootLen = strlen($docroot);

// realpath to get the full/expanded path to the files directly
$path = realpath('./files');

$dir_handle = @opendir($path) or die("Unable to open $path");

while ($file = readdir($dir_handle)) {
    if($file == "." || $file == ".." || $file == "index.php" )
        continue;

    // Create fileUrl by removing $docroot from the beginning of $path
    $fileUrl = substr($path, strpos($path, $docroot) + $docrootLen) . '/' . $file;

    echo '<a href="' . $fileUrl . '">', $file, '</a><br />';
}

?><a id="download-button"></a><?php

closedir($dir_handle);

For the "another link" part, notice I added an extra anchor tag above with the ID "download-button". 对于“另一个链接”部分,请注意,我在上面添加了一个额外的锚标记,其ID为“下载按钮”。 This will be the download button. 这将是下载按钮。 At the end of your HTML (before the closing body tag), you can add this script inside a <script> tag: 在HTML的末尾(在body标记之前),您可以在<script>标记内添加此脚本:

var links = document.getElementsByTagName("a"), linkIdx;
for( linkIdx in links ) {
    if( links[linkIdx].getAttribute("id") == "download-button" ) {
        // These are not the anchor tags you are looking for...
        continue;
    }
    links[linkIdx].addEventListener("click", function(e){
        // When the user clicks the link, don't allow the browser go to the link:
        e.preventDefault();
        document.getElementById("download-button").setAttribute("href", this.getAttribute("href"));
        document.getElementById("download-button").innerHTML = this.innerHTML;
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM