PHP to protect PDF and DOC

后端 未结 4 454
名媛妹妹
名媛妹妹 2021-01-01 03:10

I am trying to provide .pdf and .doc files to authorized users on a website. The user can only see the file selection page when logged in but this

4条回答
  •  梦毁少年i
    2021-01-01 03:46

    the answer is quite simple, @Jonnix has posted this as I was typing but I will explain a little more for you

    one put your files outside of your public HTML directory if your unable to do this look at @Andri answer for an alternative

    E.G cpanel setup

    user/public_html
        /public_html/download.php
    
    user/documents/
        /documents/file.doc
        /documents/file.pdf
    

    @dhh has posted a basic download.php php file however as your wanting to force download their things you can do like finding and supplying the correct mime type here is an extension on to his code as to the best way to 1 force download of a file, and 2 allow different file types

    download.php

    //check users is loged in and valid for download if not redirect them out
    // YOU NEED TO ADD CODE HERE FOR THAT CHECK
    // array of support file types for download script and there mimetype
    $mimeTypes = array(
        'doc' => 'application/msword',
        'pdf' => 'application/pdf',
    );
    // set the file here (best of using a $_GET[])
    $file = "../documents/file.doc";
    
    // gets the extension of the file to be loaded for searching array above
    $ext = explode('.', $file);
    $ext = end($ext);
    
    // gets the file name to send to the browser to force download of file
    $fileName = explode("/", $file);
    $fileName = end($fileName);
    
    // opens the file for reading and sends headers to browser
    $fp = fopen($file,"r") ;
    header("Content-Type: ".$mimeTypes[$ext]);
    // this header tells the browser this is a download and not to try and render if it is able to E.G images
    header('Content-Disposition: attachment; filename="'.$fileName.'"');
    
    // reads file and send the raw code to browser     
    while (! feof($fp)) {
        $buff = fread($fp,4096);
        echo $buff;
    }
    // closes file after whe have finished reading it
    fclose($fp);
    

    P.S here is a big list of mime types if you want to add support for other files https://www.freeformatter.com/mime-types-list.html

提交回复
热议问题