Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <link rel="stylesheet" type="text/css" href="stylesheet.css">
  6. <title>Image Upload</title>
  7. </head>
  8. <body>
  9.  
  10. <div>
  11. <form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" enctype="multipart/form-data">
  12. <input type="hidden" name="size" value="1000000">
  13. <div>
  14. <input type="file" name="image">
  15. </div>
  16. <div>
  17. <textarea
  18. id="text"
  19. cols="40"
  20. rows="4"
  21. name="image_text"
  22. placeholder="Say something about this photo"></textarea>
  23. </div>
  24. <div>
  25. <button type="submit" name="upload">POST</button>
  26. </div>
  27. </form>
  28.  
  29. <?php
  30. // Create database connection
  31. include("config.php");
  32. include("session.php");
  33.  
  34. // Initialize message variable
  35. $msg = "";
  36. $image_text="";
  37. $user = $_SESSION['user'];
  38.  
  39. // If upload button is clicked ...
  40. if (isset($_POST['upload'])) {
  41. // Get image name
  42. $image = $_FILES['image']['name'];
  43. // Get text
  44. $image_text = mysqli_real_escape_string($conn, $_POST['image_text']);
  45. // image file directory
  46. $target = "images/".basename($image);
  47. // Check if image file is a actual image or fake image
  48. $check = getimagesize($_FILES["image"]["tmp_name"]);
  49.  
  50. if ($check){
  51.  
  52. $stmt = $conn->prepare("INSERT INTO images (id, image, image_text ,user) VALUES (null, ?, ?, ?)");
  53. $stmt->bind_param("sss", $image, $image_text, $user);
  54. // execute query
  55. $result = $stmt->execute();
  56.  
  57. if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
  58. $msg = "Image uploaded successfully";
  59. header ('Location: uploadPhoto.php');
  60. }else{
  61. $msg = "Failed to upload image";
  62. }
  63. }
  64. else{
  65. $msg = "Only images are allowed";
  66. }
  67. }
  68. echo $msg;
  69. ?>
  70. </div>
  71. </body>
  72. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement