Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP在目录中下载spisific文件?_Php_Html - Fatal编程技术网

如何使用PHP在目录中下载spisific文件?

如何使用PHP在目录中下载spisific文件?,php,html,Php,Html,在我正在制作的web服务器中,我正在进行文件共享。下面是一些PHP代码,列出了名为“test”的目录中的所有文件 文件列表: 这将显示当前目录中所有文件的列表。单击显示的文件后,它将仅显示文件内容。但是,我不是在显示文件的内容,而是在点击文件后下载文件。经过多次尝试和错误。我找到了解决办法。需要做的只是在href html变量的末尾添加一个download属性。工作代码: <?php if ($handle = opendir('uploads/')) { while (

在我正在制作的web服务器中,我正在进行文件共享。下面是一些PHP代码,列出了名为“test”的目录中的所有文件


文件列表:

    这将显示当前目录中所有文件的列表。单击显示的文件后,它将仅显示文件内容。但是,我不是在显示文件的内容,而是在点击文件后下载文件。

    经过多次尝试和错误。我找到了解决办法。需要做的只是在href html变量的末尾添加一个
    download
    属性。工作代码:

    <?php
      if ($handle = opendir('uploads/')) {
        while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
            $thelist .= '<li><a href="uploads/'.$file.'" download>'.$file.'</a></li>';
          }
        }
        closedir($handle);
      }
    ?>
    
    
    
    听起来不错。到目前为止你试过什么?你被卡在哪里了?我可以得到一个文件列表,但我不确定如何下载它们。我觉得这样做的方法是有一个可以执行
    readfile($filename)的输入,但再一次,我不确定该如何做才有帮助?这是我用我最喜欢的搜索引擎找到的众多解决方案之一。如果没有帮助,请编辑您的问题以包含您的尝试,并澄清为什么其他教程没有帮助
    
    <?php
      if ($handle = opendir('.')) {
        while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
            $thelist .= '<li><a href="'.$file.'">'.$file.'</a></li>';
          }
        }
        closedir($handle);
      }
    ?>
    <h1>List of files:</h1>
    <ul><?php echo $thelist; ?></ul>
    
    <?php
      if ($handle = opendir('uploads/')) {
        while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
            $thelist .= '<li><a href="uploads/'.$file.'" download>'.$file.'</a></li>';
          }
        }
        closedir($handle);
      }
    ?>