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

PHP - Delete Directory With Files Instantly

semy13011

Exfiltration Specialist
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
67
Likes
99
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Delete Directory With Files Instantly using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the 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.

Lastly, 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 html>
  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">PHP - Delete Directory With Files Instantly</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <table class="table table-bordered">
  18. <thead class="alert-info">
  19. <tr>
  20. <th>Directory Name</th>
  21. <th>Action</th>
  22. </tr>
  23. </thead>
  24. <tbody style="background-color:#fff;">
  25. <?php
  26. $files

    =

    scandir

    (

    'files/'

    )

    ;
  27. foreach

    (

    $files

    as

    $file

    )

    {
  28. if

    (

    $file

    !=

    '.'

    &&

    $file

    !=

    '..'

    )

    {
  29. ?>
  30. <tr>
  31. <td><?php

    echo

    $file

    ?>

    </td>
  32. <td><a class="text-danger" href="delete.php?directory=<?php

    echo

    $file

    ?>

    ">Remove</a></td>
  33. </tr>
  34. <?php
  35. }
  36. }
  37. ?>
  38. </tbody>
  39. </table>
  40. </div>
  41. </body>
  42. </html>

Creating Main Function

This is where the main function of the application. This code will delete the directory and the file inside of it when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as delete.php.
  1. <?php
  2. if

    (

    ISSET

    (

    $_REQUEST

    [

    'directory'

    ]

    )

    )

    {
  3. $directory

    =

    $_REQUEST

    [

    'directory'

    ]

    ;
  4. if

    (

    file_exists

    (

    "files/"

    .

    $directory

    )

    )

    {
  5. removeDir(

    "files/"

    .

    $directory

    )

    ;
  6. header

    (

    "location: index.php"

    )

    ;
  7. }
  8. }

  9. function

    removeDir(

    $dir

    )

    {
  10. $items

    =

    scandir

    (

    $dir

    )

    ;
  11. foreach

    (

    $items

    as

    $item

    )

    {
  12. if

    (

    $item

    ===

    '.'

    ||

    $item

    ===

    '..'

    )

    {
  13. continue

    ;
  14. }
  15. $path

    =

    $dir

    .

    '/'

    .

    $item

    ;
  16. if

    (

    is_dir

    (

    $path

    )

    )

    {
  17. xrmdir(

    $path

    )

    ;
  18. }

    else

    {
  19. unlink

    (

    $path

    )

    ;
  20. }
  21. }
  22. rmdir

    (

    $dir

    )

    ;
  23. }
  24. ?>

There you have it we successfully created Delete Directory With Files Instantly 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

332,845

332,853

Top