File Validation in PHP

By CampCodes Administrator

Updated on:

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!

READ ALSO:   Calculate Hours Between Two Dates in PHP

apache php file upload class.upload.php tutorial client side validation in php code for file upload in php with mysql create a website file upload system csv file validation in php file attachment in php file handling in php file size validation in php file type validation in php file upload extension validation in php file upload html file upload in php file upload in php example code demo file upload in php mysql database file upload in php using oops file upload path in php file upload php file upload validation in php file upload validation in php using javascript file upload without php file validation in php form file upload php form validation php form with file upload how do you validate a file? how to retrieve uploaded file in php how to upload audio file in database using php how to upload image in folder using php how to upload image in php how to upload multiple files in php and store in database demo how to upload multiple image in php w3schools how to upload resume in php mysql how to upload video in php with example html code form with upload file and send html file upload html file upload example html file upload form html upload file to server image size validation in php image upload api in php image validation in php image validation in php using javascript inurl upload php javascript ajax file upload example javascript form validation library move uploaded file in php move uploaded file in php w3schools multiple video upload in php password validation in php password validation in php with example php 7 file upload php 7 form php check file extension before upload php check file extension of uploaded file php check file type before upload php code for image upload and display php code for upload image to database php file upload php file upload and download script free php file upload error 0 php file upload framework php file upload script github php file validation php filter_validate_email php get uploaded file extension php image upload code download php move uploaded file to another server php multiple file upload php mysql image upload script php secure file upload php secure image upload script php tutorial upload file php tutorials php upload file php upload file to another server php upload file types allowed php upload from url php upload image php upload image to directory php upload image to folder php upload image to folder example php upload multiple files php upload pdf file to server php uploader php validate file type php video upload script registration form validation in php script php upload file simple php code for image upload and display simple php file upload script download source code upload file php tutorialspoint file upload in php upload any file in php upload file upload file from one server to another server in php upload image in html and display upload image in php mysql database example upload image in php mysql database w3schools upload image php upload pdf file in php upload php file to web server url upload php script validate a file in php validation in php registration form video upload code in php free download what is form validation in php?

Leave a Comment