hi! i have problem with this rmdir. How can i remove the directory and the image inside of my dir?

this is my PHP code:

$dir="image/";
$arr=array(1=>"pig.jpg",2=>"animal.jpg");

$x=0;

while($x<2)
{
 if(unlink($dir.$arr[$x]))
   echo "success";
else
  echo "failed";

}

rmdir($dir);

but when i run the error occur.
Warning: the directory is not empty.. please help me.

Use this function:

function rmdir_recursive($dir) {
$files = scandir($dir);
foreach ($files as $file) {
  if (  ($file == ".") || ($file == "..") ) {
    continue;
    }
  $file = $dir . '/' . $file;
  if (is_dir($file)) {
    rmdir_recursive($file);
    rmdir($file);
    }
  else {
    unlink($file);
    }
  }
rmdir($dir);
}

Call it like so:

$dir = "image";
rmdir_recursive($dir);

sorry for late reply. i will try this code if i'm home. thanks for advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.