File Validation in PHP

July 1, 2020
PHP
validate file in php

In this tutorial, we will create a Simple Validate File using PHP. This code will validate a file inside your directory if it exists when the user clicks the confirm button. The system uses a PHP POST to initiate a method that certifies a file in your folder by the use of file_exist() by adding the path as a parameter. This is a user-friendly kind of program feel free to modify it.

We will be using PHP as a scripting language and interpreter, which is mainly used on any web server, including xamp, wamp, etc. It is being used on any popular website, and it has a modern technology that can be easily used by the next generation.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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, 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">PHP - Simple Validate File</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4">
            <h6 class="text-danger">Note: All files are text format</h6>
            <form action="save_file.php" method="POST" enctype="multipart/form-data">
                <div class="form-group">
                    <label>Filename</label>
                    <input type="text" name="filename" class="form-control" required="required"/>
                </div>
                <div class="form-group">
                    <label>Content</label>
                    <textarea class="form-control" name="content" style="resize:none; height:150px;" required="required"></textarea>
                </div>
 
                <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button></center>
            </form>
        </div>
        <div class="col-md-6">
            <div class="alert alert-info">Validate File here</div>
            <form method="POST" action="">
                <div class="form-group">
                    <label>File Name:</label>
                    <input type="text" class="form-control" name="file" required="required"/>
                    <br />
                    <center><button class="btn btn-primary" name="validate">Validate</button></center>
                </div>
            </form>
            <?php include'validate.php'?>
        </div>
    </div>
</body>
</html>

Creating the Save File Function
This code contains the saving function of the application. This code will store the uploaded file to its respected folder when the form is submitted. To make this just copy and write these block of codes below inside the text editor, then save it as save_file.php

<?php
    if(ISSET($_POST['save'])){
        $filename = $_POST['filename'];
        $content = $_POST['content'];
 
        $file = fopen('file/'.$filename.".txt", "w") or die("Unable to open file!");
        fwrite($file, $content);
 
        fclose($file);
 
        header("location: index.php");
    }
?>

Creating the Main Function

This code contains the main function of the application. This code will validate a file if exist when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as validate.php

<?php
    if(ISSET($_POST['validate'])){
        $filename = $_POST['file'];
        if(file_exists('file/'.$filename)){
            echo "<center class='alert-success'><h4>File ".$filename." exist!</h4></center>";
        }else{
            echo "<center class='alert-danger'><h4>File ".$filename." does not exist!</h4></center>";
        }
 
    }
?>

There you have it we successfully created Simple Validate File 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!

Facebook Comments
https://www.campcodes.com/

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

Your email address will not be published.