Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Create database connection
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbName = "test";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbName);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // Initialize message variable
- $msg = "";
- // If upload button is clicked ...
- if (isset($_POST['upload'])) {
- // Get image name
- $image = $_FILES['image']['name'];
- // Get text
- $image_text = $_POST['image_text'];
- $uploaddir = 'images/';
- $uploadfile = $uploaddir . basename($_FILES['image']['name']);
- echo "<p>";
- if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
- echo "File is valid, and was successfully uploaded.\n";
- } else {
- echo "Upload failed";
- }
- $sql = "INSERT INTO images (image, text) VALUES ('$image', '$image_text')";
- // execute query
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- }
- $query = $conn->query("SELECT * FROM images");
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Image Upload</title>
- <style type="text/css">
- #content{
- width: 50%;
- margin: 20px auto;
- border: 1px solid #cbcbcb;
- }
- form{
- width: 50%;
- margin: 20px auto;
- }
- form div{
- margin-top: 5px;
- }
- #img_div{
- width: 80%;
- padding: 5px;
- margin: 15px auto;
- border: 1px solid #cbcbcb;
- }
- #img_div:after{
- content: "";
- display: block;
- clear: both;
- }
- img{
- float: left;
- margin: 5px;
- width: 300px;
- height: 140px;
- }
- </style>
- </head>
- <body>
- <div id="content">
- <?php
- while ($row = $query->fetch_assoc()) {
- echo "<div id='img_div'>";
- echo "<img src='images/".$row['image']."' >";
- echo "<p>".$row['text']."</p>";
- echo "</div>";
- }
- ?>
- <form method="POST" action="index.php" enctype="multipart/form-data">
- <input type="hidden" name="size" value="1000000">
- <div>
- <input name="image" type="file" />
- </div>
- <div>
- <textarea
- id="text"
- cols="40"
- rows="4"
- name="image_text"
- placeholder="Say something about this image..."></textarea>
- </div>
- <div>
- <button type="submit" name="upload">POST</button>
- </div>
- </form>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement