douqing0713 2011-06-16 08:48
浏览 339
已采纳

如何加密文件下载路径

I want to allow users to download pdf file from one of my folder. Is there any way i can hide the download path from the user when user "mouseover" to the file download icon ?

Suggest any method using PHP or javascript.

  • 写回答

6条回答 默认 最新

  • dseslyh6662605 2011-06-16 09:04
    关注

    At some point you will have to give the browser the real URI so it can fetch the file. Trying to conceal it is pointless.

    I don't believe this to be true. There's a simple way to protect the files on your server from unprivileged downloading using PHP's fpassthru. If you were to have a file called download.php, with the following contents:

    <?php
    
    /**
     * Make sure the downloads are *not* in a publically accessible path, otherwise, people
     * are still able to download the files directly.
     */
    $filename = '/the/path/to/your/files/' . basename( $_GET['filename'] );
    
    /**
     * You can do a check here, to see if the user is logged in, for example, or if 
     * the current IP address has already downloaded it, the possibilities are endless.
     */
    
    
    if( file_exists( $filename ) ) {
        /** 
         * Send some headers indicating the filetype, and it's size. This works for PHP >= 5.3.
         * If you're using PHP < 5.3, you might want to consider installing the Fileinfo PECL
         * extension.
         */
        $finfo = finfo_open( FILEINFO_MIME );
        header( 'Content-Disposition: attachment; filename= ' . basename( $filename ) );
        header( 'Content-Type: ' . finfo_file( $finfo, $filename );
        header( 'Content-Length: ' . filesize( $filename ) );
        header( 'Expires: 0' );
        finfo_close( $finfo );
    
        /**
         * Now clear the buffer, read the file and output it to the browser.
         */
        ob_clean( );
        flush( );
        readfile( $filename );
        exit;
    }
    
    header( 'HTTP/1.1 404 Not Found' );
    
    echo "<h1>File not found</h1>";
    exit;
    

    You can call download.php with ?filename=test.foo, and it will download /the/path/to/your/files/test.foo, which is not publicly accessible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?