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条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)