upload and fetch image from database in php simplified coding
So you lot are thinking how to upload images to your MySQL database? Here is the Upload Image PHP MySQL Tutorial, and in this tutorial, I am going to explain how nosotros tin upload or shop images to MySQL database and then how nosotros tin can retrieve the saved images.
Contents
- i What Nosotros Need?
- two Upload Image PHP MySQL Tutorial
- 2.ane Creating Database
- 2.ii Creating a PHP Project
- two.3 Defining Constants
- ii.4 Connecting to Database
- 2.5 Class to Perform Shop and Recall Performance
- 2.6 Converting String to Paradigm
- ii.seven Uploading and Retrieving Images
- 2.viii Applying CSS to the Project
- three Upload Image PHP MySQL Source Code
What We Need?
In this mail I will be using.
- Xampp Server
- Sublime Text
You tin use other tools as well similar wamp, lamp, and notepad++ or any IDE as well. The tools don't matter the point is we will be using PHP and MySQL. So let's begin.
Upload Epitome PHP MySQL Tutorial
Creating Database
- Here I volition be storing the image directly to MySQL table. So the beginning matter nosotros need is our table. And for creating the table y'all demand to go to your PhpMyAdmin.
- Start XAMPP server and go to localhost/phpmyadmin. Here you lot need to create a database. I have created a database named SimplifiedCoding.
- Now inside this database we demand a table every bit beneath.
- For creating the above table you can utilize the following SQL.
CREATE TABLE `images` ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `description` varchar(yard) NOT Nothing, `image` longblob NOT NULL ); |
Creating a PHP Project
For this office, you can employ whatever IDE (e.yard., PHP Storm, NetBeans, Eclipse, etc.) but here I am using Sublime Text. I would recommend using PHP Tempest as I dear this IDE but at the same time if you are a beginner then avoid using an IDE to learn better.
- Go to the root directory. For XAMPP information technology is (c:/xampp/htdocs) .
- Hither create a folder named ImageUploadProject. (Though yous can give it any name information technology doesn't matter).
Defining Constants
- Now inside your project first create a php file named Constants.php. This file defines all the constants that are required for our projection.
<?php define("DB_HOST", "localhost"); define("DB_NAME", "simplifiedcoding"); define("DB_USER", "root"); define("DB_PASS", "password"); define("MAX_SIZE_ALLOWED", 1048576); //around 1mb |
Connecting to Database
- Now we will create a php class named DbConnect to connect to our Database. So create a new file named DbConnect.php and write the following code.
ane 2 3 4 5 6 7 8 9 ten 11 12 xiii xiv 15 16 17 xviii 19 20 21 22 23 24 25 26 27 28 29 xxx 31 | <?php class DbConnect{ individual $con; public function __construct(){ } public function connect(){ //getting the constants from the file require_once dirname(__FILE__). '/Constants.php'; //connecting to database $this-> con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); //if there is an error //return naught if(mysqli_connect_errno()){ repeat 'Failed to connect with database'. mysqli_connect_error(); render null; } //if everything goes well return the connection return $this-> con; } } |
Form to Perform Store and Retrieve Operation
- After database connection nosotros need to perform the store and retrieve operation. And so lets create a new class named StoreImage inside a new file named StoreImage.php.
ane 2 iii 4 5 6 7 8 nine ten 11 12 thirteen xiv 15 16 17 18 19 xx 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 36 37 38 39 twoscore 41 42 43 44 45 46 47 48 49 l 51 52 53 54 55 56 | <?php grade StoreImage{ private $con; public office __construct(){ //getting the connection file require_once dirname(__FILE__) . '/DbConnect.php'; //creating the object of the dbconnect class $db = new DbConnect; //getting the connection $this-> con = $db-> connect(); } //the method will save the image to database public function saveImage($desc, $prototype){ $stmt = $this-> con-> prepare("INSERT INTO images (description, prototype) VALUES (?, ?)"); $stmt-> bind_param("ss", $desc, $image); if($stmt-> execute()) return true; return simulated; } //the method will render all the images from the database //excluding the image, we will create a split up method for getting the prototype content //equally the image is stored in string format we demand to fetch it separately public role getAllImages(){ $stmt = $this-> con-> gear up("SELECT id, description FROM images Social club BY id DESC"); $stmt-> execute(); $stmt-> bind_result($id,$desc); $images = assortment(); while($stmt-> fetch()){ array_push($images, assortment('id'=> $id, 'desc' => $desc)); } $stmt-> close(); render $images; } //this method will return the bodily image content public function getImageContent($imageid){ $stmt = $this-> con-> gear up("SELECT image FROM images WHERE id = ?"); $stmt-> bind_param("i", $imageid); $stmt-> execute(); $stmt-> bind_result($prototype); $stmt-> fetch(); return $image; } } |
Converting String to Image
- We have stored the image as the String. Simply nosotros can't display the cord to the user, we need to display it every bit an image. So for this we will create 1 more file where we will pass the paradigm id and information technology will give us the image in epitome format.
- Then create a new file named image.php and write the following code.
<?php if(isset($_GET['id'])){ require_once dirname(__FILE__) . '/StoreImage.php'; $storeimage = new StoreImage; header('content-blazon: paradigm/jpeg'); repeat $storeimage-> getImageContent($_GET['id']); }else{ header('Location: image-upload.php'); } |
Uploading and Retrieving Images
- Now hither is our concluding operation. Create a file named epitome-upload.php and write the following code.
1 2 three 4 5 vi 7 8 9 10 11 12 13 fourteen 15 16 17 xviii 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 seventy 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 ninety 91 92 93 | <!DOCTYPE html > <?php require_once dirname(__FILE__) . '/StoreImage.php'; ?> < html > < head > < title > Upload Epitome PHP MySQL Tutorial </championship > < link rel='stylesheet' href='style.css' /> </caput > < body > < header > < h1 > Upload Paradigm PHP MySQL Tutorial </h1 > < h2 > www . simplifiedcoding . internet </h2 > </header > <?php $uploaded = ""; if($_SERVER['REQUEST_METHOD'] == 'Mail service'){ $storeimage = new StoreImage; $desc = $_POST['desc']; $image = $_FILES['paradigm']['tmp_name']; if(!($_FILES['prototype']['size'] > MAX_SIZE_ALLOWED)){ $imagecontent = file_get_contents($image); if($storeimage-> saveImage($desc, $imagecontent)){ $uploaded = 'Epitome successfully uploaded'; }else{ $uploaded = 'Some fault occurred please endeavour again'; } }else{ $uploaded = 'File exceeds the maximum size limit'; } } ?> < content > < div course='uploadpanel' > < form method="Postal service" enctype="multipart/form-data" > < div > < input type='text' name='desc' placeholder="enter prototype description" required /> </div > < div > < input type='file' name='image' accept="paradigm/*" required /> </div > < div > < push > Save Image </push > </div > </form > <?php echo !empty($uploaded)?"<div class='alert'><p>$uploaded</p></div>":""; ?> </div > < div class='imagespanel' > < h2 > Uploaded Images </h2 > <?php $storeimage = new StoreImage; $images = $storeimage-> getAllImages(); foreach($images as $image){ ?> < div form='image' style="float: left; width:100%; superlative:100%; text-align: center" > < p > < image width='250' tiptop='auto' src='<?php repeat "http://localhost/ImageUploadProject/image.php?id=".$paradigm['id']?>' /> </p > < p > <?php echo $epitome['desc']; ?> </p > </div > <?php } ?> </div > </content > < footer > < p > Copyright &re-create; 2017 < a href='https://world wide web.simplifiedcoding.net' > Simplified Coding </a > </p > </footer > </body > </html > |
- At present you can examination your project.
- Bingo! It is working absolutely fine. Additionally you can apply CSS to make it look awesome.
Applying CSS to the Project
- Create a file named way.css and write the following lawmaking.
1 2 iii 4 five 6 7 8 9 ten 11 12 13 14 fifteen xvi 17 18 19 20 21 22 23 24 25 26 27 28 29 thirty 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 lxx 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | trunk{ margin:0; padding:0; font-family unit: arial; } h1,h2{ margin:0; } header{ padding:i% 0 ane% 0; color:#cdd2d4; text-align: center; width:100%; summit:80px; background-color:#2c849e; } a{ colour:#cdd2d4; text-ornamentation: none; } a:hover{ colour: #02415a; } footer{ float:left; margin:5% 0 0 0; padding:1% 0 i% 0; color:#cdd2d4; text-align: center; width:100%; height:30px; background-colour:#2c849e; } content{ height:600px; width:100%; padding:5px; } .uploadpanel{ float:left; width:60%; text-align: center; } .uploadpanel div{ margin:2%; } .imagespanel{ text-align: heart; width:thirty%; float:left; max-tiptop:400px; overflow-y: scroll; } button{ color:#cdd2d4; border:none; border-radius: 10px; width:200px; acme:40px; background-color:#02415a; } button:hover{ cursor: pointer; background-color: #106688 } input{ font-size: 16px; width:300px; height:45px; } |
- Now you will see the project like this.
Upload Image PHP MySQL Source Code
- If you are having whatever problem you can as well go my source code from the beneath given link.
Then that's all for this Upload Image PHP MySQL Tutorial guys. If y'all constitute this post useful, and then please SHARE it. And if having whatever queries or question, and then just comment it below. Thank You 🙂
How-do-you-do, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this weblog. If you lot are an Android Developer, or y'all are learning about Android Development, so I tin help you a lot with Simplified Coding.
Source: https://www.simplifiedcoding.net/upload-image-php-mysql/
0 Response to "upload and fetch image from database in php simplified coding"
Post a Comment