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.

It is easy to upload files to MySQL database using PHP language. In this article, I will show you how to upload files to MySQL database with complete validations, file upload is the most important part of  PHP language, file upload is important because in some condition you cannot facilitate users only from your article you also need to facilitate your users through PDF files and MS Word files. 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:

  1. First, you need to create a database in MySQL, so copy the below code and go to your PHPMyAdmin database and after click on SQL tab and paste the below code in SQL query area.
  2. Create database php_crud
    
  3. Now you need to create a table inside php_crud database so copy the below code now click on the php_crud database and after click on SQL tab and paste the below code in SQL query area.
  4. 
    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
    
    
  5. After that create a folder inside htdocs folder if you are using the xampp server but if you are using wamp server then you need to create your folder inside www directory and now create a connect.php file in your main folder, now copy the below code and paste in your connect.php file
  6. 
    <?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();
    
    }
     ?>
    
  7. Now create index.php file in index.php file we have all the file upload code, so copy the below code and paste the code into your index.php file
  8. 
    <?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##]

Conclusion:

If you have any issue with file upload code then you can comment freely anytime I will give you your solution, if you link my article then please share this article with your friends thank you.

COMMENTS

Name

bootstrap,3,CSS,8,Example,2,HTML,8,login,1,mysql,3,PHP,5,
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
http://www.webtipstricks.com/2017/11/php-file-upload.html
http://www.webtipstricks.com/
http://www.webtipstricks.com/
http://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