• We just launched and are currently in beta. Join us as we build and grow the community.

Get File Extension Using PHP Source Code

Jackpott68

Ethical Surveillance Planner
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
140
Likes
47
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button></center>

  24. </form>
  25. </div>
  26. <div class="col-md-8">
  27. <form method="POST" action="">
  28. <button name="get" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-down"></span> Get Extension</button>
  29. </form>
  30. <br /><br />
  31. <div class="table-responsive">
  32. <table class="table table-bordered">
  33. <thead class="alert-info">
  34. <tr>
  35. <td>Filename</td>
  36. <td>File Extension</td>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. <?php

    include

    'get_ex.php'

    ?>
  41. </tbody>
  42. </table>
  43. </div>
  44. </div>
  45. </div>
  46. </body>
  47. </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. if

    (

    $filesize

    >

    500000

    )

    {
  8. echo

    "<script>alert('File too large to upload')</script>"

    ;
  9. echo

    "<script>window.location = 'index.php'</script>"

    ;
  10. }

    else

    {
  11. $location

    =

    "file/"

    .

    $filename

    ;
  12. if

    (

    move_uploaded_file

    (

    $filetemp

    ,

    $location

    )

    )

    {
  13. echo

    "<script>alert('File uploaded')</script>"

    ;
  14. echo

    "<script>window.location = 'index.php'</script>"

    ;
  15. }
  16. }

  17. }
  18. ?>

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. <tr>
  8. <td><?php

    echo

    $value

    ?>

    </td>
  9. <td></td>
  10. </tr>
  11. <?php
  12. }
  13. }
  14. }

    else

    {
  15. $files

    =

    scandir

    (

    'file/'

    )

    ;
  16. foreach

    (

    $files

    as

    $value

    )

    {
  17. if

    (

    $value

    !=

    '.'

    &&

    $value

    !=

    '..'

    )

    {
  18. $file

    =

    explode

    (

    '.'

    ,

    $value

    )

    ;
  19. $end

    =

    end

    (

    $file

    )

    ;
  20. ?>
  21. <tr>
  22. <td><?php

    echo

    $file

    [

    0

    ]

    ?>

    </td>
  23. <td><?php

    echo

    $end

    ?>

    </td>
  24. </tr>
  25. <?php
  26. }
  27. }
  28. }
  29. ?>

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!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

337,656

337,664

Top