zoukankan      html  css  js  c++  java
  • PHP

    文件下载目录:./uploads/

    代码:

    • 传递下载文件名称。
    • 加载配置文件中的下载文件路径。
    • 最终下载地址:../uploads/abc.txt
    <?php
    //引入配置文件
    require './config.php';
    
    //接收文件名称
    $uploadfilename = $_GET['filename'];
    
    //下载文件全路径
    $uploadFilePath = $downLoadPath . $uploadfilename;
    
    //下载文件
    download_file($uploadFilePath, null);
    
    
    /**
     * Downloader
     *
     * @param $archivo
     *  path al archivo
     * @param $downloadfilename
     *  (null|string) el nombre que queres usar para el archivo que se va a descargar.
     *  (si no lo especificas usa el nombre actual del archivo)
     *
     * @return file stream
     */
    function download_file($archivo, $downloadfilename = null) {
        if ( file_exists($archivo) ) {
            $downloadfilename = $downloadfilename !== null ? $downloadfilename : basename($archivo);
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . $downloadfilename);
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($archivo));
            ob_clean();
            flush();
            readfile($archivo);
            exit;
        } else {
            echo 'NO FILE.';
            exit();
        }
    }
  • 相关阅读:
    用户反馈
    Alpha版本测试报告
    Alpha Scrum7
    #Alpha Scrum6
    Alpha Scrum5
    #Alpha Scrum4
    Alpha Scrum3
    Alpha Scrum2
    课程总结
    实验九
  • 原文地址:https://www.cnblogs.com/KTblog/p/5236140.html
Copyright © 2011-2022 走看看