PHP MySQL Multiple Image/Files Upload and Display
![]() |
php mysql upload multiple images |
You can Upload any Image/Files Easily To Desired Folder and Save Information to Database.
##Create create 4 files and 1 folder named 'images' in same folder.
1.connect.php
2.index.php
3.upload.php
4.style.css
5. images [folder name]
![]() |
php upload multiple images |
Database Name: test
Table name: image
Using The Below SQL Commands for Creating Table 'image'
1 2 3 4 5 6 7 | CREATE TABLE IF NOT EXISTS `image` ( `id` int (11) NOT NULL AUTO_INCREMENT, ` name ` varchar (100) NOT NULL , ` size ` varchar (100) NOT NULL , `type` varchar (10) NOT NULL , PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; |
Now copy The Below Codes for Certain File.
1 2 3 4 5 6 7 8 | <?php // Creating connection test is our database name $conn = new mysqli( 'localhost' , 'root' , '' , 'test' ); // Checking connection if ( $conn ->connect_error) { die ( "Connection failed: " . $conn ->connect_error); } ?> |
Create Index page to Display Uploaded images.
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 | <head> <title>PHP MySQL Multiple Images Upload</title> <link href= "style.css" type= "text/css" rel= "stylesheet" /> </head> <body> <h2 align= "center" >Select Multiple Files to Upload</h2> <div id= "content" > <div class = "txt-heading" >Uploaded Images</div> <?php include ( "connect.php" ); include ( "upload.php" ); $result =mysqli_query( $conn , "SELECT*FROM image ORDER by ID DESC" ); $count = $result ->num_rows; if ( $count ==0){ echo "No Image Found" ; } else { while ( $row = $result ->fetch_array()){?> <ul> <li><img src= "images/<?php echo $row['name'];?>" /></li> </ul> <?php }}?> </div> </body> </html> |
Create Upload.php file to Display Upload form and Proceed Selected Images To Upload.
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 | <html> <head> <title>Upload Multiples Images Using PHP MySQL</title> <link rel= "stylesheet" type= "text/css" href= "style.css" /> </head> <div id= "form" > <form method= "post" action= "" enctype= "multipart/form-data" > <input type= "file" name= "images[]" multiple required/> <input type= "submit" name= "submit" value= "Upload" /> </form> </div> </html> <?php include ( "connect.php" ); if (isset( $_FILES [ 'images' ])){ $errors = array (); foreach ( $_FILES [ 'images' ][ 'tmp_name' ] as $key => $tmp_name ){ $file_name = $_FILES [ 'images' ][ 'name' ][ $key ]; $file_size = $_FILES [ 'images' ][ 'size' ][ $key ]; $file_tmp = $_FILES [ 'images' ][ 'tmp_name' ][ $key ]; $target = "images" ; $upload =move_uploaded_file( $file_tmp , "$target/" . $file_name ); if ( $upload ==TRUE){ $result =mysqli_query( $conn , "INSERT INTO image(name,size)VALUES('$file_name','$file_size')" ); } else { echo 'Upload Failed' ; } } if ( $upload ==TRUE){ echo "Your Files Successfully Uploaded and Information Saved in Our Database" ; } } ?> |
Create some CSS Codes to Design Our Index Page.
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 | #content{ width : 700px ; margin : auto ; } .txt-heading{ background-color : #FFD0A6 ; padding-top : 4px ; border-bottom : 2px solid green ; margin-bottom : 5px ; } ul{ list-style : none ; } ul li{ float : left ; margin-left : 5px ; margin-top : 5px ; } ul li img{ width : 200px ; height : 200px ; } #form{ width : 700px ; background : #42f48c ; padding : 5px 8px ; border-radius: 5px ; } input[type=file]{ padding : 5px 0px 5px 5px ; background : black ; border-radius: 5px ; } input[type=submit]{ background : black ; color : white ; padding : 6px 8px ; font-weight : bold ; cursor : pointer ; border-radius: 5px ; } |
Now You have Successfully Created PHP MySQL Multiple Files/Image Upload and Display System.
Please Upload Image First to Get result.
Thank You Visiting Our Blog.
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.