Check File Exist Using PHP
Submitted by razormist on Tuesday, June 30, 2020 - 19:51.
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.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </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">Check File Exist Using PHP</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-8">
- <form method="POST" action="">
- <div class="form-inline">
- <label>File Name:</label>
- <input type="text" class="form-control" name="file" required="required"/><button class="btn btn-primary" name="check">Check</button>
- </div>
- </form>
- <?php include'check_file.php'?>
- <br />
- <table class="table table-bordered">
- <thead class='alert-info'>
- <tr>
- <th><center>Files in folder</center></th>
- </tr>
- </thead>
- <tbody>
- <?php
- foreach ($files as $file){
- if($file != '.' && $file !='..'){
- $path = "file/".$file;
- ?>
- <tr>
- <td><?php echo $file?></td>
- </tr>
- <?php
- }
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </body>
- </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- <?php
- $filename = $_POST['file'];
- echo "<center class='alert-success'><h4>File ".$filename." is available!</h4></center>";
- }else{
- echo "<center class='alert-danger'><h4>File ".$filename." is not available!</h4></center>";
- }
- }
- ?>
Add new comment
- 425 views