0

How to get image folder -> subfolder name -> from MySQL URL:

images/5d049d4ac96e6/image.jpg

and delete it with all content. At the moment code delete image.jpg file only from image subdirectory but what I need is to delete also subfolder /5d049d4ac96e6/

$stmt = $db->prepare('SELECT postImage FROM blog_posts_seo WHERE postID = :postID') ;
    $stmt->execute(array(':postID' => $_GET['delpost']));
    while($row = $stmt->fetch()){
    $delImage =  dirname(__FILE__, 2) . '/'.$row['postImage'];
    unlink($delImage);
    //rmdir(?????);
    }

How to extract this part of mysql url: /5d049d4ac96e6/

Rasa Mohamed
  • 882
  • 1
  • 6
  • 14
Axīc
  • 21
  • 1
  • 6

1 Answers1

0

Before deleting the folder, delete all it's files and folders. Here is the function for that use it in while loop

function rrmdir($dir) {
  if (is_dir($dir)) {
    $objects = scandir($dir);
    foreach ($objects as $object) {
      if ($object != "." && $object != "..") {
        if (filetype($dir."/".$object) == "dir") 
           rrmdir($dir."/".$object); 
        else unlink   ($dir."/".$object);
      }
    }
    reset($objects);
    rmdir($dir);
  }
}

In your while loop

while($row = $stmt->fetch()){
    $dir = dirname($row['postImage']).'/';
    rrmdir($dir);
}
Rasa Mohamed
  • 882
  • 1
  • 6
  • 14
  • nothing happen, all files are unlink, folder is empty, should be deleted.. – Axīc Jun 15 '19 at 11:35
  • @Axīc nothing happen mean, is deleting files alone! or deleting nothing!? I tested in windows worked fine. – Rasa Mohamed Jun 15 '19 at 11:38
  • this part of code : unlink files: $delImage = dirname(__FILE__, 2) . '/'.$row['postImage']; unlink($delImage); is ok, works, added code you shared but no luck, /5d049d4ac96e6/ still on place empty.. – Axīc Jun 15 '19 at 11:43
  • @Axic did u copied function `rrmdir($dir)`? – Rasa Mohamed Jun 15 '19 at 11:49
  • yes, I place it to function.php files as rest of functions, but no luck, already try $dir = 'image/subfolder/'; rrmdir($dir); to test, no luck also.. – Axīc Jun 15 '19 at 11:55