Check File Exist Using PHP

In this code we will tackle about Check File Exist using PHP. The code will check the file if exist in the folder when a filename is entered. This is a user-friendly kind of program feel free to modify it. To learn more about this tutorial, just follow the step below.

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 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">Check File Exist Using PHP</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-8">
  18.                         <form method="POST" action="">
  19.                                 <div class="form-inline">
  20.                                         <label>File Name:</label>
  21.                                         <input type="text" class="form-control" name="file" required="required"/><button class="btn btn-primary" name="check">Check</button>
  22.                                 </div>
  23.                         </form>
  24.                         <?php include'check_file.php'?>
  25.                         <br />
  26.                         <table class="table table-bordered">
  27.                                 <thead class='alert-info'>
  28.                                         <tr>
  29.                                                 <th><center>Files in folder</center></th>
  30.                                         </tr>
  31.                                 </thead>
  32.                                 <tbody>
  33.                                         <?php
  34.                                                 $files = scandir('file/');
  35.                                                 foreach ($files as $file){
  36.                                                         if($file != '.' && $file !='..'){
  37.                                                                 $path = "file/".$file;
  38.                                         ?>
  39.                                         <tr>
  40.                                                 <td><?php echo $file?></td>
  41.                                         </tr>
  42.                                         <?php
  43.                                                         }
  44.                                                 }
  45.                                         ?>
  46.                                 </tbody>
  47.                         </table>
  48.                 </div>
  49.         </div>
  50.  
  51. </body>
  52. </html>

Creating the Main Function

This code contains the main function of the application. This will dynamically check whether a certain file is existed. To make this just copy and write these block of codes below inside the text editor, then save it as check_file.php
  1. <?php
  2.         if(ISSET($_POST['check'])){
  3.                 $filename = $_POST['file'];
  4.                 if(file_exists('file/'.$filename)){
  5.                         echo "<center class='alert-success'><h4>File ".$filename." is available!</h4></center>";
  6.                 }else{
  7.                         echo "<center class='alert-danger'><h4>File ".$filename." is not available!</h4></center>";
  8.                 }
  9.        
  10.         }
  11. ?>
There you have it we successfully created Check 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