PHP - Check If File Exist

In this tutorial we will create a Check If File Exist using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the 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 Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Check If File Exist</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add File</button>
  18.                 <br style="clear:both;"/>
  19.                 <div class="col-md-3"></div>
  20.                 <div class="col-md-6">
  21.                         <form method="POST" action="">
  22.                                 <div class="form-group">
  23.                                         <label>File Name:</label>
  24.                                         <input type="text" class="form-control" name="file" required="required"/>
  25.                                         <br />
  26.                                         <center><button class="btn btn-primary" name="check">Check</button></center>
  27.                                 </div>
  28.                         </form>
  29.                         <?php
  30.                                 if(ISSET($_POST['check'])){
  31.                                         $filename = $_POST['file'];
  32.                                         if(file_exists('file/'.$filename)){
  33.                                                 echo "<center class='alert-success'><h4>File ".$filename." exist!</h4></center>";
  34.                                         }else{
  35.                                                 echo "<center class='alert-danger'><h4>File ".$filename." does not exist!</h4></center>";
  36.                                         }
  37.                                
  38.                                 }
  39.                         ?>
  40.                 </div>
  41.         </div>
  42.         <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  43.                 <div class="modal-dialog" role="document">
  44.                         <form action="save_file.php" method="POST" enctype="multipart/form-data">
  45.                                 <div class="modal-content">
  46.                                         <div class="modal-body">
  47.                                                 <div class="col-md-3"></div>
  48.                                                 <div class="col-md-6">
  49.                                                         <div class="form-group">
  50.                                                                 <label>Filename</label>
  51.                                                                 <input type="text" name="filename" class="form-control" required="required"/>
  52.                                                         </div>
  53.                                                         <div class="form-group">
  54.                                                                 <label>Content</label>
  55.                                                                 <textarea class="form-control" name="content" style="resize:none; height:150px;" required="required"></textarea>
  56.                                                         </div>
  57.                                                 </div>
  58.                                         </div>
  59.                                         <div style="clear:both;"></div>
  60.                                         <div class="modal-footer">
  61.                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  62.                                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  63.                                         </div>
  64.                                 </div>
  65.                         </form>
  66.                 </div>
  67.         </div>
  68. </body>
  69. <script src="js/jquery-3.2.1.min.js"></script>
  70. <script src="js/bootstrap.js"></script>
  71. </html>

Creating PHP Query

This is the main function of the application. This code create the file inside the directory, just to be able to check whether the file exist inside the directory. To do that copy and write these inside the text editor, then save it as save_file.php,
  1. <?php
  2.         if(ISSET($_POST['save'])){
  3.                 $filename = $_POST['filename'];
  4.                 $content = $_POST['content'];
  5.                
  6.                 $file = fopen('file/'.$filename.".txt", "w") or die("Unable to open file!");
  7.                 fwrite($file, $content);
  8.                
  9.                 fclose($file);
  10.                
  11.                 header("location: index.php");
  12.         }
  13. ?>
There you have it we successfully created Check If File Exist using PHP. 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!

Add new comment