How to create, view and delete the directory using PHP

Words
372
Reading
2 min
Listen
Play
9y

What Will I Learn?

  • You will learn how to create a directory from php file
  • You will learn how to view the directory
  • You will learn how to delete the directory

Requirements

  • Basic Knowledges About PHP
  • Basic Knowledges About PHP
  • WebServer ( In this tutorial I use XAMPP )

Difficulty

  • Basic

Tutorial Contents

PHP has provided a function to view, create and change the directory or folder. With open this PHP file you can do all mentioned above without need to do manually. Hacker usually use this PHP function to create a PHP SHELL file for uploading to a victim webserver, and with accessing this file they can monitor all directory or all file in the victim webserver. For more detail lets follow steps bellow:

  • Reactive your webserver

  • Create a new folder in xampp/htdocs if you use xampp. While you use Linux create it at var/www.

  • Open the previous folder from your text editor. and create new php file save as index.php

  • Add HTML element as usual

<html>
<head>
<title>Directory</title>
</head>
<body>
</body>
</html>
  • Create an input element and a button element
Direktory : <input type="text" name="folder">
 <input type="submit" value="Create">
  • Open the PHP to write the PHP code
<?php
?>
  • To view the all directory we can use opendir() function like this
$path=".";
$dir=opendir($path) or die ("can't open directory");
  • Display all directory using While
while($file=readdir($dir)){
echo "$file ";
}
  • Add delete button and link it to delete.php to delete the directory
while($file=readdir($dir)){
echo "$file DELETE
"
; }
  • To hide the operation file ( the file is use to manipulate the directory) you can use if function, So the code become like this .
while($file=readdir($dir)){
    if($file=="."||$file==".."|| $file=="index.php"|| $file=="create.php" || $file=="delete.php")
    continue;
    echo "$file    DELETE
"
; }
  • Close the dir
closedir($dir);
  • Open new file and create a PHP file save as create.php
  • Get the name folder that you will create from input element in index.php
$dir=$_POST['folder'];
  • Create the directory using mkdir() function with the permision is 777
mkdir($dir, 777);
  • reload to index.php
header("location: index.php");
  • Create new file and save it as delete.php
  • Get the name folder that you will delete from delete button
$folder=$_GET['dir'];
  • Delete the directory using rmdir() function
rmdir($folder);
  • Reload to index.php
header("location: index.php")
  • Save all file and try to run

  • Create new directory

  • You can see the directory just created by you under the input element

  • To Delete the directory click delete button
    image.png

  • Full code you can get bellow:
    index.php

<html>
    <head></head>
    <body>
        <form action="create.php" method="POST">
            Direktory : <input type="text" name="folder">
            <input type="submit" value="Create">
        </form>
    </body>
</html>
<?php
$path=".";
$dir=opendir($path) or die ("can't open directory");
while($file=readdir($dir)){
    if($file=="."||$file==".."|| $file=="index.php"|| $file=="create.php" || $file=="delete.php")
    continue;
    echo "$file    DELETE
"
; } closedir($dir); ?>

Create.php

<?php
$dir=$_POST['folder'];
mkdir($dir, 777);
header("location: index.php");
?>

delete.php

<?php
$folder=$_GET['dir'];
rmdir($folder);
header("location: index.php")
?>

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

How to create, view and delete the directory using PHP | Ecency