Get File Extension Using PHP Source Code
Submitted by razormist on Friday, February 28, 2020 - 09:28.
In this tutorial we will create a Get File Extension using PHP. This code will display the extension of a file when the user click the button. The code use PHP POST method to call a specific method that will display the file extension of a file using explode(). This is a user-friendly kind of program feel free to modify it.
We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.
get_ex.php
There you have it we successfully created Get File Extension 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 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">Get File Extension Using PHP Source Code</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-4">
- <form action="save_file.php" method="POST" enctype="multipart/form-data">
- <div class="form-group">
- <label>Filename</label>
- <input type="file" name="file" class="form-control" required="required"/>
- </div>
- <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button></center>
- </form>
- </div>
- <div class="col-md-8">
- <form method="POST" action="">
- <button name="get" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-down"></span> Get Extension</button>
- </form>
- <br /><br />
- <div class="table-responsive">
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <td>Filename</td>
- <td>File Extension</td>
- </tr>
- </thead>
- <tbody>
- <?php include 'get_ex.php'?>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </body>
- </html>
Creating the Main Function
This code contains the main function of the application. This code will display the extension of a file 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 shown below. save_file.php- <?php
- $filename = $_FILES['file']['name'];
- $filetype = $_FILES['file']['type'];
- $filetemp = $_FILES['file']['tmp_name'];
- $filesize = $_FILES['file']['size'];
- if($filesize > 500000){
- echo "<script>alert('File too large to upload')</script>";
- echo "<script>window.location = 'index.php'</script>";
- }else{
- $location ="file/".$filename;
- echo "<script>alert('File uploaded')</script>";
- echo "<script>window.location = 'index.php'</script>";
- }
- }
- }
- ?>
- <?php
- foreach ($files as $value) {
- if ($value != '.' && $value != '..'){
- ?>
- <tr>
- <td><?php echo $value?></td>
- <td></td>
- </tr>
- <?php
- }
- }
- }else{
- foreach ($files as $value) {
- if ($value != '.' && $value != '..'){
- ?>
- <tr>
- <td><?php echo $file[0]?></td>
- <td><?php echo $end?></td>
- </tr>
- <?php
- }
- }
- }
- ?>
Add new comment
- 183 views