PHP File Upload With Validations

File upload is now the common feature in all PHP applications, because 90% websites in the world have the file upload feature.

PHP File Upload

It is easy to upload files to MySQL database using PHP language. In this today article, I will show how you can upload files to MySQL database using PDO, file upload is the most important part of  PHP language, file upload is the important part of the PHP website because in the educational and the business websites file upload is very necessary. so to provide PDF files or MS Word files first you need to upload these files after you can provide the download link to users. for example in school management system website file upload is the most important because when the student's results are announced then you must to provide the download link to students in MS Excel format so after he/she can search result in offline mode. so in this article, we will do file upload with complete variations.
file upload

Follow Steps:

  • Database

    Create database php_crud
    

  • Table

    
    CREATE TABLE `file` (
     `id` int(11) NOT NULL AUTO_INCREMENT,
     `name` varchar(255) NOT NULL,
     PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1
    
    

  • Connect.php

    
    <?php 
    try{
    $db = new PDO("mysql:host=localhost;dbname=php_crud","root","");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e){
     echo $e->getMessage();
    
    }
     ?>
    

  • Index.php

    
    <?php 
    include 'connection/connect.php';
    if(isset($_POST['upload_file'])){
        $file_name = $_FILES['img_file']['name'];
     $tmp_name  = $_FILES['img_file']['tmp_name'];
     $extension = array("jpeg","jpg","png","pdf","docx","doc");
     $ex        = explode(".", $file_name);
     $last      = end($ex);
     $errors    = array();
     if(!in_array($last, $extension)){
        $errors['invalid'] = "Invalid image extension";
     }else{
        $store     = "files/";// files are store here
        move_uploaded_file($tmp_name, "$store/$file_name");
        $Query = $db->query("INSERT INTO file(name) VALUES ('$file_name')");
        if($Query){
         $errors['success'] = "Your file is successfully uploaded";
        }else{
         $errors['query_not_word'] = "Your file is not work";
    
        }
     }
    }
     ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
     <meta charset="UTF-8">
     <title>File Upload</title>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
     <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
    </head>
    <body>
     <div class="container" style="margin-top: 60px;">
      <div class="row justify-content-center">
       <div class="col-md-4">
        <form action="" method="POST" enctype="multipart/form-data">
         <div class="form-group">
          <?php if(isset($errors)): ?>
           <ul>
            <?php foreach($errors as $error): ?>
            <li><?php echo $error; ?></li>
           <?php endforeach; ?>
           </ul>
          <?php endif; ?>
         </div>
         <div class="form-group">
          <input type="file" name="img_file" class="form-control" required="">
         </div><!-- form-group -->
         <div class="form-group">
          <input type="submit" value="Upload" name="upload_file" class="btn btn-success">
         </div><!-- form-group -->
        </form>
       </div><!-- col -->
      </div><!-- row -->
      <div class="row">
       <div class="col-md-8">
        <?php 
    
                    function display_files(){
                     GLOBAL $db;
                   $Display = $db->query("SELECT * FROM file ORDER BY id DESC");
         if($Display->rowCount() == 0){
          echo "Sorry we dont have any file";
         }else{
          echo "<table class='table'>
          <tr>
                <th>Icon</th>
                <th>File</th>
                <th>Formate</th>
    
          </tr>";
    
          while($r = $Display->fetch(PDO::FETCH_OBJ)):
                     $file_name = $r->name;
                     $extension = explode(".", $file_name);
                     $ext = end($extension);
                     if($ext == "pdf"){
                      echo "<tr>
                          <td><i class='fa fa-file-pdf-o'></td><td><a href='files/$file_name'>$file_name</a></td>
                          <td>PDF</td>
                      </tr>";
                     }
                      if($ext == "docx" OR $ext == "doc"){
                      echo "<tr>
                          <td><i class='fa fa-file-word-o'></td><td><a href='files/$file_name'>$file_name</a></td>
                          <td>DOCX</td>
                      </tr>";
                     }
                      if($ext == "jpg" OR $ext == "jpeg"){
                      echo "<tr>
                          <td><i class='fa fa-file-image-o'></td><td>
                          <img src='files/$file_name' style='width:60px;height:60px;'></td>
                          <td>JPG</td>
                      </tr>";
                     }
    
          endwhile;
          echo "</table>";
         }
                    }
                    display_files();
         ?>
       </div>
      </div>
     </div><!-- container -->
    </body>
    </html>
    
Download the file upload complete source code from the below link

[Download Souce Code ##download##]

COMMENTS

Name

ajax,1,HTML & CSS,12,PHP,6,
ltr
item
webtipstricks: PHP File Upload With Validations
PHP File Upload With Validations
File upload is now the common feature in all PHP applications, because 90% websites in the world have the file upload feature.
https://4.bp.blogspot.com/-x-RqZXTvtdc/WtN-iC3e4OI/AAAAAAAABfw/bdbJm8nhqP0hBC-yiszK4Ko_8bTnlDpgQCLcBGAs/s640/PHP%2BfILE%2BUPLOAD%2BWITH%2BVALIDATIONS.png
https://4.bp.blogspot.com/-x-RqZXTvtdc/WtN-iC3e4OI/AAAAAAAABfw/bdbJm8nhqP0hBC-yiszK4Ko_8bTnlDpgQCLcBGAs/s72-c/PHP%2BfILE%2BUPLOAD%2BWITH%2BVALIDATIONS.png
webtipstricks
https://www.webtipstricks.com/2017/11/php-file-upload.html
https://www.webtipstricks.com/
https://www.webtipstricks.com/
https://www.webtipstricks.com/2017/11/php-file-upload.html
true
4027471107773963481
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy