HTMLify
Img Upload to db and display
Views: 621 | Author: djdj
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | <!DOCTYPE html> <html> <head> <title>File Upload</title> </head> <body> <form action="#" method="post" enctype="multipart/form-data"> <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload File" name="submit"> </form> </body> </html> <?php $msg = false; $conn = mysqli_connect('server_name', 'user_name', 'password', 'database'); if(!$conn){ die(mysqli_connect_error()); } else{ // echo'success'; // if(isset($_SERVER['REQUEST_METHOD'] == 'POST')){ if(isset($_POST["submit"])) { $targetDirectory = "uploads/"; $targetFile = $targetDirectory . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $fileType = strtolower(pathinfo($targetFile,PATHINFO_EXTENSION)); // Check if file already exists if (file_exists($targetFile)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 500000) { echo "Sorry, your file is too large."; $uploadOk = 0; } // Allow certain file formats if($fileType != "txt" && $fileType != "pdf" && $fileType != "png" && $fileType != "jpg" && $fileType != "jpeg"){ echo "Sorry, only TXT, PDF, DOC files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)) { echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; $msg = true; } else { echo "Sorry, there was an error uploading your file."; } } } // } if($msg){ $sql = 'INSERT INTO img (img) VALUES ("'.basename( $_FILES['fileToUpload']['name']).'")'; $result = mysqli_query($conn, $sql); if($result){ echo '<script>alert("You file submit successfully!!");</script>'; } } } ?> <table border="1" style="margin:30px;"> <tr> <h2>All Img</h2> <td>Sno</td> <td>Img</td> </tr> <tr> <?php $query = "SELECT * FROM img"; $r = mysqli_query($conn, $query); if(mysqli_num_rows($r) > 0){ while($row = mysqli_fetch_assoc($r)){ echo' <td>'.$row['sno'].'</td> <td><img src="https://dj.000.pe/talksta/uploads/'.$row['img'].'" height=100px; width=100px></td> </tr> '; } } ?> </table> |