Advertisement
ayami123

PDO How to insert

Jan 28th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. <?php
  2.  
  3. /* sql
  4. CREATE TABLE `images` (
  5. `hash` CHAR() NOT NULL PRIMARY KEY,
  6. `name` VARCHAR(128) NOT NULL UNIQUE,
  7. `mime` VARCHAR(128) NOT NULL,
  8. `image` BLOB
  9. )ENGINE=INNODB CHARSET=UTF8MB4 COLLATE=utf8mb4_unicode_ci;
  10. */
  11.  
  12. // default message
  13. $message = 'Please select an image.';
  14.  
  15. // make sure a file was uploaded
  16. if (isset($_FILES['image'])) {
  17.  
  18. try {
  19. // make sure it's really an image (this will throw an error if it's not)
  20. $image = new Imagick($_FILES['image']['tmp_name']);
  21. // we'll format the image as a png. you might also want to resize, etc.
  22. $image->setImageFormat('png');
  23.  
  24. // SEE https://gist.github.com/adrian-enspired/385c6830ba2932bc36a2
  25. $host = '';
  26. $dbname = '';
  27.  
  28. $dsn = "mysql:host={$host};dbname={$dbname};charset=UTF8MB4";
  29. $user = '';
  30. $pass = '';
  31. $options = [
  32. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  33. PDO::ATTR_EMULATE_PREPARES => false
  34. ];
  35. $pdo = new PDO($dsn, $user, $pass, $options);
  36. $insert = $pdo->prepare("INSERT INTO `images` (`hash`, `name`, `mime`, `image`) VALUES (:hash, :name, :mime, :image)");
  37.  
  38. // the "name" is the name the user gave this image. we can use it for lookups.
  39. // the "hash" is a checksum for the image (we can use this as a filename to cache the image on disk)
  40. // the "mime" is the mime-type of the image
  41. // the "image" is the image blob (binary string; the actual image file)
  42. $hash = md5($image->getImageBlob());
  43. $params = [
  44. 'name' => $_FILES['image']['name'],
  45. 'hash' => $hash,
  46. 'mime' => 'image/png',
  47. 'image' => $image->getImageBlob()
  48. ];
  49.  
  50. // execute the INSERT statement
  51. $insert->execute($params);
  52. $message = 'Image stored successfully.';
  53. } catch (ImagickExcpetion $e) {
  54. $message = 'Invalid file upload. Please upload an image (jpg, png, etc.)';
  55.  
  56. // for development, you can see the exception message like so:
  57. // echo $e; exit(1);
  58. // uncomment if you want to.
  59. // NEVER do this on a "live" (production) website.
  60. } catch (PDOException $e) {
  61. $message = 'Unable to store your image.';
  62. // echo $e; exit(1);
  63. } catch (Exception $e) {
  64. $message = 'Unknown error.';
  65. // echo $e; exit(1);
  66. }
  67. }
  68.  
  69. // below is the page template
  70. ?><!doctype html>
  71. <html>
  72. <head>
  73. <meta charset="utf-8">
  74. <title>Image Upload</title>
  75. </head>
  76. <body>
  77. <h1>Image Upload</h1>
  78. <p><?= htmlspecialchars($message) ?></p>
  79. <form action="?" method="post" enctype="multipart/form-data">
  80. <input type="file" name="image">
  81. <button>Upload</button>
  82. </form>
  83. </body>
  84. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement