4

I am deploying a website from where user can purchase the pdfs. now i am searching for the way for storing the pdfs so that it only can be downloaded when payment is done. I have came across one way in which i can store the pdfs in to the Mysql database and generate the path to it when required credentials fulfill.

Is there any other way to do this and link to the pdf file should be dynamic and encrypted so that other links to the other books can't be predicted.

and the server side language I am using is PHP

neophyte
  • 6,540
  • 2
  • 28
  • 43
nikhil
  • 877
  • 3
  • 11
  • 36
  • store them outside the web root and serve with a php file –  Sep 28 '13 at 10:36
  • @Dagon...thnx for reply..can you please provide me the link for tutorial of the given concept – nikhil Sep 28 '13 at 10:37
  • 1
    possible duplicate of [Creating a Secure File Hosting Server for PDFs](http://stackoverflow.com/questions/10743850/creating-a-secure-file-hosting-server-for-pdfs) –  Sep 28 '13 at 10:39
  • [Deny direct access to a folder by htaccess](http://stackoverflow.com/questions/9282124/deny-direct-access-to-a-folder-and-file-by-htaccess) – Joop Eggen Sep 28 '13 at 10:41

3 Answers3

3
  1. You need to store the files somewhere outside your website root like mentioned by Dagon. When file is uploaded use move_uploaded_file to move it. You can name the file anything you want (within OS limits) and keep the real name in the database.
  2. Then when the user has payed for the books, add the books the user has payed for to a table in a db.
  3. Give the user a list of all the books he has payed for like: /download/filename.pdf
  4. Add a mod_rewrite if you use Apache (or equivalent for other web servers) where /download/.* is redirected to download.php or a controller.
  5. On the download page, check if user is logged in and has access to the file. If not, redirect to purchase page for that book.
  6. If download is ok set header for the http status you need: Content-Length, Content-Type, Date, Status (200), maybe Content-Encoding.
  7. Use readfile to output the file to the end user.
OIS
  • 9,833
  • 3
  • 32
  • 41
  • thanks for reply..now i have a question that how to upload a file outside root directory....i want to upload files from browser interface which will be scripted using php not from upload clients like filezilla... – nikhil Sep 28 '13 at 10:56
  • 1
    @nikhil answered your question – OIS Sep 28 '13 at 11:06
2

I would :

  • Deny any access to the files -- i.e. use a .htaccess file (That way, no-one has access to the file)

  • Develop a PHP script that would :

    • receive a file identifier (a file name, for instance ; or some identifier that can correspond to the file)
    • authenticate the users (with some login/password fields), against the data stored in the database if the user is valid, and has access to the file (This is if different users don't have access to the same set of files), read the content of the file from your PHP script, and send it the the user.

The advantage is that your PHP script has access to the DB -- which means it can allow users to log-in, log-out, it can use sessions, ...

Here is another answer from a stack user that fits this problem: Creating a Secure File Hosting Server for PDFs

Community
  • 1
  • 1
OBV
  • 1,169
  • 1
  • 12
  • 25
2

is there any other way to do this and link to the pdf file should be dynamic and encrypted so that other links to the other books can't be predicted.

  1. The best way, is after payment generate a key to the file.
  2. create a page like this www.site.com/download.php?key=key (and here you don't need to have id of the book, because by the key you can check on the database what is the book the customer purchased.
  3. inside the download.php read the key, query the database to find which file is linked with the key
  4. read the file, and send it to the customer. This is, if the key is valid, you will send the php headers as content type as being pdf, and (the php code) read the file in binary and send it in the message body.

I hope this code helps

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>
SQL.injection
  • 2,607
  • 5
  • 20
  • 37