douan2478 2010-08-26 13:19
浏览 206
已采纳

使用php下载临时链接的文件

I am working on a project where the user would be able to buy media files. after the payment is processed I would like to allow them to download the file. I guess it is safe to say that I should have a temporary link to the files. one that is linked to the IP of the user and perhaps a timestamp? the problem is I dont know where to start with that. First of all. is this the way to do it? if so..how do I proceed using php. ( i guess I dont need the exact script just hints on how to do it although if there is an existing script I would not mind) thank you.

  • 写回答

3条回答 默认 最新

  • dongshao2967 2010-08-26 13:33
    关注

    Since you are going to handle the file in PHP you might aswell use a login to check if the user has purchased the file, other than that the code should look a little like this:

    header('Content-Type: application/force-download');
    
    $file = new File(intval($_GET['id']));
    $fileLocation = dirname(__FILE__) . "/../../upload/fileArchive/" . $file->id . "." . $file->type;
    
    header('Content-Length:' . filesize($fileLocation));
    header("Content-Disposition: inline; filename=\"".$file->name."\"");
    
    $filePointer = fopen($fileLocation,"rb");
    fpassthru($filePointer);
    

    Taken from production and tested

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

报告相同问题?