Page 1 of 1

Deleting a folder with PHP

Posted: Wed Jul 07, 2004 11:28 am
by nube
Hi all,

I'm very new to PHP... I mean VERY

In a nutshell, I need help using PHP to delete a folder, and subfolder within that folder... and every file in each.

A little background...

I've been using ASP for a couple of years now in web development and have run across a problem. For various reasons I cannot get ASP to delete a folder on the web server using File System Object.

I've read up on some PHP, but it's still very much like reading greek or latin to me still. I need to get this one page finished... and then hopefully get the heck out of ASP and start learning PHP for good.

I need to know a little bit about how to use unlink() to delete a folder.

The folder will contain .jpg images, as well as a subdirectory called "small" that also contains .jpg thumbnails.

Typically I've used this code in ASP (where "sold" is the variable containing the name of the folder):

Code: Select all

if oDelete.FolderExists (server.mappath("" & sold)) then
   oDelete.DeleteFolder (server.mappath("" & sold)), true
else 
   response.redirect ("page.asp")
end if
But on this particular server we (myself and the host) can't get the permissions to work correctly (probably because it's IIS, LOL)

I want to do the same thing in PHP and see if it will work.

What I want to do is use my same ASP pages for deleting database entries that pertain to this directory, then redirect to a PHP page and pass the directory name along...

The only thing on the PHP page will be the delete code, and a redirect back. I have no knowledge of PHP functions or syntax, etc.

I'm hoping PHP will be able to do what ASP cannot in this case... or at least I want to give it a try.

This is the closest thing to helpful that I've found so far...
http://us3.php.net/unlink

and it's still confusing me...

Does all this make sense?

Is it nearly as difficult as I've made it sound?

Any help is greatly appreciated...

Posted: Wed Jul 07, 2004 12:13 pm
by John Cartwright

Code: Select all

<?php

if (is_dir($dir))
{
//unlink($dir); not sure if unlink can delete folders so try
rmdir($dir); 
}
else
{
header("page.asp");
}

?>

Posted: Wed Jul 07, 2004 12:31 pm
by nube
Phenom... thanks for the reply...

Is it really that simple?

Dang, I was expecting something far more difficult...

So, to make sure I'm understanding correctly...

rmdir($dir)

(remove directory) (directory name)

The ($dir) must be referring to my directory?

How can I grab that name from a variable (passed from another page)

For example, in ASP I would use (to keep it simple)

<%
dir=request.querystring("dir")
%>


does $dir do the same thing?


would I simply use this to pass the variable from the asp page?

<%
response.redirect ("page.php?dir=1234")
%>

then ($dir) would = "1234" ?

Also, is there a PHP equivelant to ASP's server.mappath, or do I need to specify the path?

Holy crap I should have learned this stuff years ago... every step I realize what a mistake it was to learn ASP first...

:cry:

Thanks again for your response... I really appreciate the help so far.

nube

Posted: Wed Jul 07, 2004 12:36 pm
by feyd
$_GET['dir']

Posted: Wed Jul 07, 2004 12:43 pm
by nube
amazing...

My whole problem possibly solved in under an hour and only a few form posts.

Thanks for the wonderful help!

Posted: Wed Jul 07, 2004 1:48 pm
by John Cartwright
:P PHP > ASP!!!!!!!!!!!

Posted: Wed Jul 07, 2004 2:10 pm
by nube
:wink:

too bad I'm late in figuring that out.

Posted: Wed Jul 07, 2004 2:13 pm
by hedge
hmm, I'm pretty sure that won't work if the directory has content within it. You usually have to walk the tree and delete everything... maybe that was the problem you had when using asp.

I would consider shelling out and running a 'rmdir /S' command

Posted: Wed Jul 07, 2004 2:43 pm
by Zorth
Many things in PHP and C++ are much simpler than you would imagion. I use to think that sending a message through a socket would require hundreds of lines of codes while it really only required the use of one function after they were set up.

Posted: Wed Jul 07, 2004 4:56 pm
by d_d
Check the first user contribution
http://uk2.php.net/unlink

Posted: Thu Jul 08, 2004 1:13 pm
by nube
Okay... so if the rmdir above doesn't remove everything... I should try this:

Code: Select all

<?php
//Example usage:
deltree ($basedir.$dir); // base directory is now empty
rmdir ($basedir.$dir); // base directory is now gone.

function deltree($dir)&#123;
 $d = dir($dir);
 while($f = $d->read() )&#123;
  if($f != "." && $f != "..")&#123;
   if(is_dir($dir.$f))&#123;
   deltree($dir.$f."/");
   rmdir($dir.$f);
   &#125; // if
   if(is_file($dir.$f))
   unlink($dir.$f);
   &#125;// if
  &#125;// while
 $d->close();
&#125;
?>
I would assume $basedir.$dir should be the full path to the directory, and the directory itself..?

I'm still a little confused on how to translate ASP's server.mappath into PHP.

Using FSO the method I outlined above works great... deletes everything in the folder as long as you specify "true" at the end. I've used it numerous times in the past without any problems. This server won't retain permissions for some reason... they keep resetting.

I'll keep playing with this and see if I can get it to work. At the moment I'm waiting for the admin to set PHP permissions for this account.

Again, thanks for all the great help

nube