Advertisement
Guest User

$284632828282$

a guest
Jan 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. <?php
  2.  
  3. class BobDemo {
  4.  
  5. const DB_HOST = 'localhost';
  6. const DB_NAME = 'membership';
  7. const DB_USER = 'root';
  8. const DB_PASSWORD = '';
  9.  
  10. /**
  11. * PDO instance
  12. * @var PDO
  13. */
  14. private $pdo = null;
  15.  
  16. /**
  17. * Open the database connection
  18. */
  19. public function __construct() {
  20. // open database connection
  21. $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME);
  22.  
  23. try {
  24. $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD);
  25. //for prior PHP 5.3.6
  26. //$conn->exec("set names utf8");
  27. } catch (PDOException $e) {
  28. echo $e->getMessage();
  29. }
  30. }
  31.  
  32. /**
  33. * insert blob into the files table
  34. * @param string $filePath
  35. * @param string $mime mimetype
  36. * @return bool
  37. */
  38. public function insertBlob($filePath, $mime) {
  39. $blob = fopen($filePath, 'rb');
  40.  
  41. $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)";
  42. $stmt = $this->pdo->prepare($sql);
  43.  
  44. $stmt->bindParam(':mime', $mime);
  45. $stmt->bindParam(':data', $blob, PDO::PARAM_LOB);
  46.  
  47. return $stmt->execute();
  48. }
  49.  
  50. /**
  51. * update the files table with the new blob from the file specified
  52. * by the filepath
  53. * @param int $id
  54. * @param string $filePath
  55. * @param string $mime
  56. * @return bool
  57. */
  58. function updateBlob($id, $filePath, $mime) {
  59.  
  60. $blob = fopen($filePath, 'rb');
  61.  
  62. $sql = "UPDATE files
  63. SET mime = :mime,
  64. data = :data
  65. WHERE id = :id;";
  66.  
  67. $stmt = $this->pdo->prepare($sql);
  68.  
  69. $stmt->bindParam(':mime', $mime);
  70. $stmt->bindParam(':data', $blob, PDO::PARAM_LOB);
  71. $stmt->bindParam(':id', $id);
  72.  
  73. return $stmt->execute();
  74. }
  75.  
  76. /**
  77. * select data from the the files
  78. * @param int $id
  79. * @return array contains mime type and BLOB data
  80. */
  81. public function selectBlob($id) {
  82.  
  83. $sql = "SELECT mime,
  84. data
  85. FROM files
  86. WHERE id = :id;";
  87.  
  88. $stmt = $this->pdo->prepare($sql);
  89. $stmt->execute(array(":id" => $id));
  90. $stmt->bindColumn(1, $mime);
  91. $stmt->bindColumn(2, $data, PDO::PARAM_LOB);
  92.  
  93. $stmt->fetch(PDO::FETCH_BOUND);
  94.  
  95. return array("mime" => $mime,
  96. "data" => $data);
  97. }
  98.  
  99. /**
  100. * close the database connection
  101. */
  102. public function __destruct() {
  103. // close the database connection
  104. $this->pdo = null;
  105. }
  106.  
  107. }
  108.  
  109. $blobObj = new BobDemo();
  110.  
  111. // test insert gif image
  112. // $blobObj->insertBlob('images/php-mysql-blob.gif',"image/gif");
  113.  
  114.  
  115. $a = $blobObj->selectBlob(1);
  116. header("Content-Type:" . $a['mime']);
  117. echo $a['data'];
  118.  
  119.  
  120. // test insert pdf
  121. //$blobObj->insertBlob('pdf/php-mysql-blob.pdf',"application/pdf");
  122. //$a = $blobObj->selectBlob(2);
  123. // save it to the pdf file
  124. //file_put_contents("pdf/output.pdf", $a['data']);
  125. // $a = $blobObj->selectBlob(2);
  126. // header("Content-Type:" . $a['mime']);
  127. // echo $a['data'];
  128. // replace the PDF by gif file
  129. $blobObj->updateBlob(2, 'images/php-mysql-blob.gif', "image/gif");
  130.  
  131. $a = $blobObj->selectBlob(2);
  132. header("Content-Type:" . $a['mime']);
  133. echo $a['data'];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement