Dear friends in this article we will tell you How to upload images using php. So i hope you like our article because here you can get the source code free and this code you can use in your projects. Guys if you learn more interesting articles then go to the search bar and type article name then you can find easily more interested articles.
Friends, in this article we will know how to upload dynamic images using php, so let’s understand. First of all you have to take a code editor in which we can write code, we have used vs code in this tutorial, you can also use any other editor if you want. So first we will go to the editor and create a php file for the connectivity of the database, after that we will go to the database and create a database then create a table inside it which has 3 fields to keep. Then we have to go to the connectivity file and connect to the database. Something like this.
How to make authentication system using php?
How to create CRUD operation using php?
How to make typing tool using php?
<?php $conn = mysqli_connect('localhost','root','','dynamic'); if(!$conn){ echo "database not connected"; } ?>
Now we have to create a php file for a form so that we can submit the data from that form and add that data to our database with the help of php so that we can easily get that data inside any file for make this upload images using php. Something like this.
<form method="POST" enctype="multipart/form-data"> <h1>Dynamic Images Upload</h1> <input type="text" name="title" placeholder="title"><br><br> <input type="file" name="file"><br><br> <button type="submit" name="submit">Submit</button> </form>
Friends, we also wrote some css code in this tutorial upload images using php, so that this profile is looking attractive, if you also want to design the profile and upload the image, then you can copy this css code of ours and use it in your project. css code is given below.
.profile{ width: 300px; height: 300px; padding: 30px; background-color: #fff; border-radius: 10px; } .profile h2{ font-family: verdana; text-align: center; margin-bottom: 20px; color: blue; } .profile .img{ width: 100px; height: 100px; border-radius: 50%; border: 2px solid red; margin: auto; overflow: hidden; } .profile .img img{ width: 100%; height: 100%; } .profile h4{ font-family: verdana; text-align: center; margin-top: 30px; }
PHP Code..
if (isset($_POST['submit'])) { $title = $_POST['title']; $file = $_FILES["file"]; // print_r($file); $filename = $file['name']; $filetype = $file['type']; $filepath = $file['tmp_name']; $error = $file['error']; $filesize = $file['size']; if ($error == 0) { $fileLocation = 'img/' . $filename; move_uploaded_file($filepath, $fileLocation); $insertQ = "INSERT INTO `dynamictable` (`title`,`img`) VALUES ('$title','$fileLocation')"; $query = mysqli_query($conn, $insertQ); if ($query) { echo "insert successfully"; } else { echo "somthing wrong"; } } else { echo "not insert"; } }