Advertisement
Guest User

index.php

a guest
Feb 4th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1. <?php
  2.   // Create database connection
  3.     $servername = "localhost";
  4.     $username = "root";
  5.     $password = "";
  6.     $dbName = "test";
  7.  
  8.     // Create connection
  9.     $conn = new mysqli($servername, $username, $password, $dbName);
  10.  
  11.     // Check connection
  12.     if ($conn->connect_error) {
  13.         die("Connection failed: " . $conn->connect_error);
  14.     }
  15.   // Initialize message variable
  16.   $msg = "";
  17.  
  18.   // If upload button is clicked ...
  19.   if (isset($_POST['upload'])) {
  20.     // Get image name
  21.     $image = $_FILES['image']['name'];
  22.     // Get text
  23.     $image_text = $_POST['image_text'];
  24.  
  25.     $uploaddir = 'images/';
  26.     $uploadfile = $uploaddir . basename($_FILES['image']['name']);
  27.  
  28.     echo "<p>";
  29.  
  30.     if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
  31.     echo "File is valid, and was successfully uploaded.\n";
  32.     } else {
  33.     echo "Upload failed";
  34.     }
  35.  
  36.  
  37.     $sql = "INSERT INTO images (image, text) VALUES ('$image', '$image_text')";
  38.       // execute query
  39.     if ($conn->query($sql) === TRUE) {
  40.         echo "New record created successfully";
  41.     } else {
  42.         echo "Error: " . $sql . "<br>" . $conn->error;
  43.     }
  44.   }
  45.   $query = $conn->query("SELECT * FROM images");
  46. ?>
  47. <!DOCTYPE html>
  48. <html>
  49. <head>
  50. <title>Image Upload</title>
  51. <style type="text/css">
  52.    #content{
  53.     width: 50%;
  54.     margin: 20px auto;
  55.     border: 1px solid #cbcbcb;
  56.    }
  57.    form{
  58.     width: 50%;
  59.     margin: 20px auto;
  60.    }
  61.    form div{
  62.     margin-top: 5px;
  63.    }
  64.    #img_div{
  65.     width: 80%;
  66.     padding: 5px;
  67.     margin: 15px auto;
  68.     border: 1px solid #cbcbcb;
  69.    }
  70.    #img_div:after{
  71.     content: "";
  72.     display: block;
  73.     clear: both;
  74.    }
  75.    img{
  76.     float: left;
  77.     margin: 5px;
  78.     width: 300px;
  79.     height: 140px;
  80.    }
  81. </style>
  82. </head>
  83. <body>
  84. <div id="content">
  85.   <?php
  86.     while ($row = $query->fetch_assoc()) {
  87.       echo "<div id='img_div'>";
  88.         echo "<img src='images/".$row['image']."' >";
  89.         echo "<p>".$row['text']."</p>";
  90.       echo "</div>";
  91.     }
  92.   ?>
  93.   <form method="POST" action="index.php" enctype="multipart/form-data">
  94.     <input type="hidden" name="size" value="1000000">
  95.     <div>
  96.         <input name="image" type="file" />
  97.  
  98.     </div>
  99.     <div>
  100.       <textarea
  101.         id="text"
  102.         cols="40"
  103.         rows="4"
  104.         name="image_text"
  105.         placeholder="Say something about this image..."></textarea>
  106.     </div>
  107.     <div>
  108.         <button type="submit" name="upload">POST</button>
  109.     </div>
  110.   </form>
  111. </div>
  112. </body>
  113. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement