PHP - Simple Upload Files Using PDO & Ajax
Submitted by razormist on Thursday, January 10, 2019 - 20:53.
In this tutorial we will create a Simple Upload Files using PDO & Ajax PHP is a server-side scripting language designed primarily for web development. PDO stands for PHP Data Objects. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.
upload.php
script.js
Save this file inside the js directory.
There you have it we successfully created a Simple Upload Files using PDO & Ajax. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Getting started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.- <?php
- $conn = new PDO( 'mysql:host=localhost;dbname=db_file', 'root', '');
- if(!$conn){
- }
- ?>
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Simple Upload Files Using PDO & Ajax</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <form class="form-inline" method="POST">
- <label><input type="file" id="file" class="form-control"/></label>
- <button type="button" class="btn btn-primary" id="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
- <br /><br />
- <div id="result" style="display:none;"></div>
- </form>
- <div class="table-responsive">
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Location</th>
- </tr>
- </thead>
- <tbody id="data" style="background-color:#fff;"></tbody>
- </table>
- </div>
- </div>
- <script src="js/jquery-3.2.1.min.js"></script>
- <script src="js/script.js"></script>
- </body>
- </html>
Creating the Main Function
This code contains the main function of the application. This code will upload the file using ajax request to prevent the web browser from refreshing. To do this just copy and write these block of codes as shown below inside the text editor, then save it as show below. data.php- <?php
- require 'conn.php';
- $query = $conn->prepare("SELECT * FROM `file` ORDER BY `filename` ASC");
- $query->execute();
- while($fetch = $query->fetch()){
- ?>
- <tr>
- <td><?php echo $fetch['filename']?></td>
- <td><?php echo $fetch['location']?></td>
- </tr>
- <?php
- }
- ?>
- <?php
- require_once 'conn.php';
- try{
- $file_name = $_FILES['file']['name'];
- $file_temp = $_FILES['file']['tmp_name'];
- $path = "upload/".$file_name;
- $name = $exp[0];
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = "INSERT INTO `file` (`filename`, `location`) VALUES ('$name', '$path')";
- }
- }catch(PDOException $e){
- echo $e->getMessage();
- }
- $conn = null;
- echo "success";
- ?>
- $(document).ready(function(){
- display_data();
- function display_data(){
- $.ajax({
- url: 'data.php',
- type: 'POST',
- data: {res: 1},
- success: function(data){
- $('#data').html(data);
- }
- });
- }
- $('#upload').on('click', function(){
- var file = $('#file');
- var file_length = file[0].files.length;
- var file_data = file.prop('files')[0];
- $('#result').css('display', '');
- if(file_length > 0){
- var form_data = new FormData();
- form_data.append('file', file_data);
- $.ajax({
- url: 'upload.php',
- type: 'POST',
- processData: false,
- contentType: false,
- data: form_data,
- success: function(data){
- if(data == "success"){
- $("#result").attr('class', 'alert alert-success');
- $("#result").html("File uploaded");
- display_data();
- }
- }
- });
- }else{
- $("#result").attr('class', 'alert alert-danger');
- $("#result").html("Please select a file first");
- }
- });
- });
Add new comment
- 545 views