PHP unlink is deleting everything

Asked

Viewed 167 times

2

I have this code: And I wanted to delete a file based on the address for directory and file you are given, the problem is that it is deleting everything despite the argument (address) that I step only belong to a file

DB.php

public function deleteFromFolder($path) {
        unlink($path);
}

public function fetchAllRandomImageHighlightsAdmin() {
    $sql = ("SELECT `image_path` FROM `highlight_image`);
    $result = $this->_db->prepare($sql);
    $result->execute();

    return $result->fetchAll(PDO::FETCH_OBJ);
}

public function fetchAllSeveralWorkAdmin() {
    $sql = ("SELECT `image_path` FROM `several_work_image`);
    $result = $this->_db->prepare($sql);
    $result->execute();

    return $result->fetchAll(PDO::FETCH_OBJ);
}

Edit.php (delete Random image)

            if (isset($_POST['idRandom'])) {
            $id = $_POST['idRandom'];

            foreach ($dataBase->fetchAllRandomImageHighlightsAdmin() as $deleteFile) {
                $dataBase->deleteFromFolder($deleteFile->image_path);
            }

            $dataBase->deleteImageWorkRandom($id);
            header('Location: editLisa.php');
        }

Edit.php (delete several work image)

            if (isset($_POST['idSeveral'])) {

            $id = $_POST['idSeveral'];

            foreach ($dataBase->fetchAllSeveralWorkAdmin() as $deleteFile) {
                $dataBase->deleteFromFolder($deleteFile->image_path);
            }

            $dataBase->deleteImageSeveralWork($id);
            header('Location: editLisa.php');
        }
  • 1

    Doing a debug, what is the value of $path enough for you in your duties deleteFromFolderRandomImage and deleteFromFolderRandomImage?

  • 1

    They’re both different, I just saw, but it doesn’t delete the database of the two... Then it’s okay

  • 1

    But the value of $path what’s coming up is actually the path of a file? When you say you are deleting everything, you mean you are deleting all the contents of the directory where the file is?

  • 1

    By the way I apologize if you do an echo inside the foreach instead of '$Database->deleteFromFolderSeveralWork' or anything, but if you do before the foreach already appear all paths related to the table

  • I edited the code, simplified it a little... However the results are the same

  • Right, the echo that you did was to present what? Inside the foreach make a echo $deleteSeveralFile->image_path; and see what is returned. Just to check if the content of this attribute is valid and what you are expecting...

  • It returns nothing, it’s not even printing an echo 'hey', however it deletes from the printer... I don’t understand...

  • I think I already know... the problem is that I am doing fetch to everything inside that table and delete each path, I need to add an sql that selects only to the path of the selected id, I will update as soon as it is... Thank you so much @Adriano

  • Okay, if you can resolve let me know... I was going to ask you to check using print_r() the value of $dataBase->fetchAllSeveralWorkAdmin() to check if the array is correct with the actual image path. But if you can solve, let us know... =)

  • 4

    Miguel, here we do not use "solved" in the title. Please publish a reply and mark it as correct. Check out the [tour] of the site.

Show 5 more comments

1 answer

1


The problem is that I was selecting all the elements of each table and was deleting them one by one. The correct way is:

DB.php

public function selectRandomImageToDelete($id) {
    $sql = ("SELECT `image_path` FROM `highlight_image` WHERE `id` = $id");
    $result = $this->_db->prepare($sql);
    $result->execute();

    return $result->fetchAll(PDO::FETCH_OBJ);
}

public function selectSeveralImageToDelete($id) {
    $sql = ("SELECT `image_path` FROM `several_work_image` WHERE `id` = $id");
    $result = $this->_db->prepare($sql);
    $result->execute();

    return $result->fetchAll(PDO::FETCH_OBJ);
}

Edit.php (delete Random image)

if (isset($_POST['idRandom'])) {
            $id = $_POST['idRandom'];

            foreach ($dataBase->selectRandomImageToDelete($id) as $deleteFile) {
                $dataBase->deleteFromFolder($deleteFile->image_path);
            }

            $dataBase->deleteImageWorkRandom($id);
            header('Location: editLisa.php');
        }

Edit.php (delete several image)

if (isset($_POST['idSeveral'])) {

            $id = $_POST['idSeveral'];

            foreach ($dataBase->selectSeveralImageToDelete($id) as $deleteFile) {
                $dataBase->deleteFromFolder($deleteFile->image_path);
            }

            $dataBase->deleteImageSeveralWork($id);
            header('Location: editLisa.php');
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.