-1

Please i want to do the same as the selected answer in pdf

How to retrieve images from MySQL database and display in an html tag

I want to retreive pdf in MySQL database and display it browser here is my code :

<?php

      $id = $_GET['id'];
      // do some validation here to ensure id is safe

      $link = mysql_connect("localhost", "root", "");
      mysql_select_db("workshopinternational");
      $sql = "SELECT fichier FROM inscrits WHERE id=$id";
      $result = mysql_query($sql);
      $row = mysql_fetch_assoc($result);
      mysql_close($link);

      header("Content-type: image/jpg");
      //header('Content-type: application/pdf');

      //readfile('original.pdf');
      echo $row['fichier'];

      ?> 
Community
  • 1
  • 1
Rodrigo
  • 327
  • 6
  • 25

1 Answers1

3

Keeping files in a database is probably not a great idea for a lot of reasons. While databases like mysql are great for relational data, they aren't very efficient at quickly fetching files. Instead:

  1. Each file should be kept in a directory with a unique ID. So, mydocument.docx might be in files/012312234232/mydocument.docx
  2. We'll store the file name, the unique ID, and some other handy metadata (author, who owns it, permissions, etc) in the database.
  3. When someone wants the file, map it to the disk, and either redirect them, or fopen() the file, fread() the file, and then send it as your response, after setting your 'content-type' header to either match the file, or as application/octect-stream

You should remember to check that you have enough disk space before saving files, and make sure your unique IDs are in fact unique - such as a primary key from your database table. And, make sure to sanitize anything that goes into the database queries.

Also, you should switch to using the php mysqli plugin (or similar) instead of the mysql_* methods, as they are considered insecure and deprecated.

Kyros
  • 512
  • 2
  • 5