File Validation In PHP | CampCodes ...

File Validation in PHP

July 1, 2020
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

And, this is the link for the bootstrap that I used for the layout design

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">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="">Sourcecodester</a>
    <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">
                    <input type="text" name="filename" class="form-control" required="required"/>
                <div class="form-group">
                    <textarea class="form-control" name="content" style="resize:none; height:150px;" required="required"></textarea>
                <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button></center>
        <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>
            <?php include'validate.php'?>

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

        $filename = $_POST['filename'];
        $content = $_POST['content'];
        $file = fopen('file/'.$filename.".txt", "w") or die("Unable to open file!");
        fwrite($file, $content);
        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

        $filename = $_POST['file'];
            echo "<center class='alert-success'><h4>File ".$filename." exist!</h4></center>";
            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!


PHP - Simple Validate File Source Code 337.39 KB 48 downloads

Please go back to the description page. Just Click the Red Download Button on the...

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. Required fields are marked *