duanjing4623 2019-07-03 09:22
浏览 230
已采纳

用于通过网站从包含服务器上指定文件夹中的文件的列表页面下载文件的PHP脚本

I need the option to show other people who are not programmers the variety of files they could download from the FTP server folder (I use WinSCP)

Now the problem with the code is that if you write one specific file name in the PHP script, you can download it without a problem. But what i need is for them to upload(which works fine) but also for them to be able(some other time or someone else) to choose from the previous uploaded files and to download that specific one that they selected.

So the precise thing we need is to open the folder, show all the files in there and then to be able to choose from one of those files and download it.

Does anyone know how to solve this or is it even possible?

<?php
//uploads is the folder name at the ftp server
if ($handle = opendir('uploads/')) {
    while (false !== ($entry = readdir($handle)))
    {
        //
        if ($entry != "." && $entry != "..") {
            //download.php is the file where we write the code
            echo "<a href='download.php?file=".$entry."'>".$entry."</a>
";
        }
    }

    closedir($handle);//
}

// we are trying to get the files by listing it with .$file variable
$file = basename($_GET['file']);
$file = 'uploads/'.$file;

//here we are 
$mimi = $file;

if(!mimi_exists($file)){ // file does not exist
    die('file not found');
} else {
    header("Cache-Control: public"); //
    header("Content-Description: File Transfer");//
    header("Content-Disposition: attachment; filename=$file");//
    header("Content-Type: application/zip");//
    header("Content-Transfer-Encoding: binary");//

    // read the file from disk
    readfile($file);
}
?>

With this previous code, the file names (from the folder) are listed and shown as links but there is no action when we click on them.

  • 写回答

1条回答 默认 最新

  • dsaf32131 2019-07-03 09:52
    关注

    The code that

    1. Lists the files
    2. Downloads a file

    must be separate.


    (While not strictly necessary), easy way is to put them into separate files, like list.php and download.php. The list.php will generate a list of download links, which will points to download.php (what you already do in the opendir ... closedir block):

    if ($handle = opendir('uploads/')) {
        while (false !== ($entry = readdir($handle)))
        {
            //
            if ($entry != "." && $entry != "..") {
                //download.php is the file where we write the code
                echo "<a href='download.php?file=".$entry."'>".$entry."</a>
    ";
            }
        }
    
        closedir($handle);//
    }
    

    The download.php will contain the rest of your code:

    // we are trying to get the files by listing it with .$file variable
    $file = basename($_GET['file']);
    $filepath = 'uploads/'.$file;
    
    if(!file_exists($filepath)){ // file does not exist
        die('file not found');
    } else {
        header("Cache-Control: public"); //
        header("Content-Description: File Transfer");//
        header("Content-Disposition: attachment; filename=$file");//
        header("Content-Type: application/zip");//
        header("Content-Transfer-Encoding: binary");//
    
        // read the file from disk
        readfile($filepath);
    }
    

    (I have replaced your strange mimi_exists with file_exists and fixed the issues that internal "upload" file path got accidentally sent to the browser)


    A similar question: List and download clicked file from FTP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥150 HDMI分路器LT86102 的输出在890MHz频点处EMC超标8DB
  • ¥36 如何安全的迁移用户C盘用户文件(戴尔电脑)
  • ¥15 druid(相关搜索:数据库|防火墙)
  • ¥15 大一python作业
  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥60 如何鉴定微信小程序数据被篡改过
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥20 手机截图相片分辨率降低一半