Guest User

Untitled

a guest
Sep 19th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. <form action="upload.php" method="post" enctype="multipart/form-data">
  2. Select image to upload:
  3. <input type="file" name="image"/>
  4. <input type="submit" name="submit" value="UPLOAD"/>
  5. </form>
  6.  
  7. <?php
  8. if(isset($_POST["submit"])){
  9. $check = getimagesize($_FILES["image"]["tmp_name"]);
  10. if($check !== false){
  11. $image = $_FILES['image']['tmp_name'];
  12. $imgContent = addslashes(file_get_contents($image));
  13.  
  14. /*
  15. * Insert image data into database
  16. */
  17.  
  18. //DB details
  19. $dbHost = 'localhost';
  20. $dbUsername = 'root';
  21. $dbPassword = '';
  22. $dbName = 'dictionary';
  23.  
  24. //Create connection and select DB
  25. $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
  26.  
  27. // Check connection
  28. if($db->connect_error){
  29. die("Connection failed: " . $db->connect_error);
  30. }
  31.  
  32. $dataTime = date("Y-m-d H:i:s");
  33.  
  34. //Insert image content into database
  35. $insert = $db->query("INSERT into images (image, created) VALUES ('$imgContent', '$dataTime')");
  36. if($insert){
  37. echo "File uploaded successfully.";
  38. }else{
  39. echo "File upload failed, please try again.";
  40. }
  41. }else{
  42. echo "Please select an image file to upload.";
  43. }
  44. }
  45.  
  46. CREATE TABLE `images` (
  47. `id` int(11) NOT NULL AUTO_INCREMENT,
  48. `image` longblob NOT NULL,
  49. `created` datetime NOT NULL,
  50. PRIMARY KEY (`id`)
  51. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Add Comment
Please, Sign In to add comment