--
-- Table structure for table `uploads`
--
CREATE TABLE `uploads` (
`id` int(11) NOT NULL,
`file_name` varchar(150) NOT NULL,
`upload_time` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `uploads`
--
INSERT INTO `uploads` (`id`, `file_name`, `upload_time`) VALUES
(1, 'car.jpg', '2021-02-17 08:10:47');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `uploads`
--
ALTER TABLE `uploads`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `uploads`
--
ALTER TABLE `uploads`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX.
https://github.com/jquery-form/form
bootstrap progressbar https://getbootstrap.com/docs/4.0/components/progress/
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 | //index.php <!DOCTYPE html> <html> <head> <title>Jquery Ajax PHP Mysql File Upload Progress Bar</title> <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js" integrity= "sha384-qlmct0AOBiA2VPZkMY3+2WqkHtIQ9lSdAsAn5RUJD/3vA5MKDgSGcdmIv4ycVxyn" crossorigin= "anonymous" ></script> <!--<script src= "/static/js/jquery.form.js" ></script>--> </head> <body> <div class = "container" > <br /> <h3 align= "center" >Jquery Ajax PHP Mysql File Upload Progress Bar</h3> <br /> <div class = "panel panel-default" > <div class = "panel-heading" ><b>Jquery Ajax PHP Mysql File Upload Progress Bar</b></div> <div class = "panel-body" > <form id= "uploadImage" action= "upload.php" method= "post" > <div class = "form-group" > <label>File Upload</label> <input type= "file" name= "uploadFile" id= "uploadFile" accept= ".jpg, .png" /> </div> <div class = "form-group" > <input type= "submit" id= "uploadSubmit" value= "Upload" class = "btn btn-info" /> </div> <div class = "progress" > <div class = "progress-bar progress-bar-striped bg-success" role= "progressbar" aria-valuenow= "0" aria-valuemin= "0" aria-valuemax= "100" ></div> </div> <div id= "targetLayer" style= "display:none;" ></div> </form> <div id= "loader-icon" style= "display:none;" ><img src= "images/loader.gif" /></div> </div> </div> </div> <script> $(document).ready( function (){ $( '#uploadImage' ).submit( function (event){ if ($( '#uploadFile' ).val()) { event.preventDefault(); $( '#loader-icon' ).show(); $( '#targetLayer' ).hide(); $(this).ajaxSubmit({ target: '#targetLayer' , beforeSubmit: function (){ $( '.progress-bar' ).width( '50%' ); }, uploadProgress: function (event, position, total, percentageComplete) { $( '.progress-bar' ).animate({ width: percentageComplete + '%' }, { duration: 1000 }); }, success: function (){ $( '#loader-icon' ).hide(); $( '#targetLayer' ).show(); }, resetForm: true }); } return false; }); }); </script> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //upload.php <?php include ( 'dbcon.php' ); if (! empty ( $_FILES )) { if ( is_uploaded_file ( $_FILES [ 'uploadFile' ][ 'tmp_name' ])) { $source_path = $_FILES [ 'uploadFile' ][ 'tmp_name' ]; $target_path = 'upload/' . $_FILES [ 'uploadFile' ][ 'name' ]; if (move_uploaded_file( $source_path , $target_path )) { $filename = $_FILES [ 'uploadFile' ][ 'name' ]; $today = date ( "Y-m-d h:i" ); $sql = "INSERT INTO uploads(file_name,upload_time) VALUES ('$filename','$today')" ; $conn ->query( $sql ); echo '<p><b>File successfully uploaded ' . $filename . ' to the database!</b><p>' ; echo '<p><img src="' . $target_path . '" class="img-thumbnail" width="300" height="250" /></p>' ; } } } ?> |
1 2 3 4 5 6 7 | //dbcon.php <?php $conn = new mysqli( 'localhost' , 'root' , '' , 'testingdb' ); if ( $conn ->connect_error) { die ( 'Error : (' . $conn ->connect_errno . ') ' . $conn ->connect_error); } ?> |