How to create, view and delete the directory using PHP

in utopian-io •  7 years ago  (edited)

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 "<a href='$file'>$file</a> ";
}
  • Add delete button and link it to delete.php to delete the directory
while($file=readdir($dir)){
echo "<a href='$file'>$file</a> <a href='delete.php?dir=$file'><button>DELETE</button></a><br>";
}
  • 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 "<a href='$file'>$file</a>    <a href='delete.php?dir=$file'><button>DELETE</button></a><br>";
}
  • 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 "<a href='$file'>$file</a>    <a href='delete.php?dir=$file'><button>DELETE</button></a><br>";
}
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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

sobat sonan nan droneuh?neu vote long sige

Ka mulai lake teurimong guenaseh bak si done sang lawetyoo

pat si done ??

Dak meuroeh bahasa aceh sang habeh ta baca..hehe

@sogata, Upvote for supporting you.

Mantapp nyan bg berbagi ilmee 👍

this is great thanks for the information

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • There is a very similar content which may cause marking this post as plagiarized. It's up to decision of my supervisor.
    Similar content
  • Even if it's not plagiarized, the contribution cannot be approved since tutorials with some simple usage of functions which do not add value to the community, are not allowed.
  • Since you complained about the decision last time, I want to inform you in detail. In Utopian tutorials category, we only reward tutorials with detailed instructions which really teach a technical aspect of the project. There are too many simple tutorials being sent and rejection rate in the category is about 70%. I would suggest that you should contribute with more technical and high quality contributions to get rewarded.

You can contact us on Discord.
[utopian-moderator]

Thank a lot my bro for your suggestion and advice

Hello bro I need a help from you i think you won't support fakes but I found a fake person will you help me @prowhale give him a falg why because my frnd sent a bid amount to vote but they are useing for personal use

What can I do??

  ·  7 years ago Reveal Comment

Generic comments could be mistaken for spam.

Tips to avoid being flagged

Thank You! ⚜

  ·  7 years ago Reveal Comment