Skip to main content
Known Participant
June 9, 2009
Question

delete folder & files from form entry

  • June 9, 2009
  • 2 replies
  • 1852 views

How would I enter a folder name in a form and then have the specified folder and it's files deleted? Can I do this with one php page? Site is running on a linux server. I looked at php functions for rmdir and rmdirr but not sure how to enter the value needed for $dir.

This topic has been closed for replies.

2 replies

June 10, 2009

To create directory from the input form of a page, you can try like this.. Assume your input is $_POST['dir'].

if ($_POST['dir']) {

$dir = './dirName/';

     //Create if directory not exists
     if (!file_exists($dir)) {
          mkdir($dir);
     }

}

Known Participant
June 10, 2009

Thanks QiQi86, but I need to DELETE the folder that I input not create it. Can you help?

June 10, 2009

Owh, okay. Maybe u need to change the code a bit. Try this :

if ($_POST['dir']) {

     //Delete folder if exists
     if (file_exists($_POST['dir'])) {
          rmdir($_POST['dir']);
     }

}

To know more about this rmdir() function, you may look at HERE

Participating Frequently
June 9, 2009

There's some sample code near the bottom of this page that should help.

http://www.php.net/manual/en/function.unlink.php

Known Participant
June 9, 2009

So do I use a Get or Post in the form or what? Can all the code be on the same page? Do I need to just use ($dir) as the variable or would I use something like (.$_POST['dir'].)  I always research php.net before asking something here, but I am new at this and do not always understand how to put step 1 and step 2 together.

Participating Frequently
June 10, 2009

>So do I use a Get or Post in the form or what?

Of course. Use whichever is more appropriate for your particular application.

>Can all the code be on the same page?

Are you asking if the script can be on the same page as the form? Sure.

>Do I need to just use ($dir) as the variable or would I use something like (.$_POST['dir'].)

In the script I referenced, $dir is the parameter declared in the delete function. Call the delete function by passing it the request variable $_POST['dir']