Get File Extension Using PHP Source Code

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.

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.
  1. <!DOCTYPE>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">Get File Extension Using PHP Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form action="save_file.php" method="POST" enctype="multipart/form-data">
  19. <div class="form-group">
  20. <label>Filename</label>
  21. <input type="file" name="file" class="form-control" required="required"/>
  22. </div>
  23.  
  24.  
  25. <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button></center>
  26.  
  27. </form>
  28. </div>
  29. <div class="col-md-8">
  30. <form method="POST" action="">
  31. <button name="get" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-down"></span> Get Extension</button>
  32. </form>
  33. <br /><br />
  34. <div class="table-responsive">
  35. <table class="table table-bordered">
  36. <thead class="alert-info">
  37. <tr>
  38. <td>Filename</td>
  39. <td>File Extension</td>
  40. </tr>
  41. </thead>
  42. <tbody>
  43. <?php include 'get_ex.php'?>
  44. </tbody>
  45. </table>
  46. </div>
  47. </div>
  48. </div>
  49. </body>
  50. </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
  1. <?php
  2. if(ISSET($_POST['save'])){
  3. $filename = $_FILES['file']['name'];
  4. $filetype = $_FILES['file']['type'];
  5. $filetemp = $_FILES['file']['tmp_name'];
  6. $filesize = $_FILES['file']['size'];
  7.  
  8.  
  9. if($filesize > 500000){
  10. echo "<script>alert('File too large to upload')</script>";
  11. echo "<script>window.location = 'index.php'</script>";
  12. }else{
  13. $location ="file/".$filename;
  14. if(move_uploaded_file($filetemp, $location)){
  15. echo "<script>alert('File uploaded')</script>";
  16. echo "<script>window.location = 'index.php'</script>";
  17. }
  18. }
  19.  
  20. }
  21. ?>
get_ex.php
  1. <?php
  2. if(!ISSET($_POST['get'])){
  3. $files = scandir('file/');
  4. foreach ($files as $value) {
  5. if ($value != '.' && $value != '..'){
  6.  
  7. ?>
  8. <tr>
  9. <td><?php echo $value?></td>
  10. <td></td>
  11. </tr>
  12. <?php
  13. }
  14. }
  15. }else{
  16. $files = scandir('file/');
  17. foreach ($files as $value) {
  18. if ($value != '.' && $value != '..'){
  19. $file = explode('.', $value);
  20. $end = end($file);
  21. ?>
  22. <tr>
  23. <td><?php echo $file[0]?></td>
  24. <td><?php echo $end?></td>
  25. </tr>
  26. <?php
  27. }
  28. }
  29. }
  30. ?>
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!

Add new comment