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');
}
Doing a debug, what is the value of $path enough for you in your duties deleteFromFolderRandomImage and deleteFromFolderRandomImage?
– Adriano Leal
They’re both different, I just saw, but it doesn’t delete the database of the two... Then it’s okay
– Miguel
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?
– Adriano Leal
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
– Miguel
I edited the code, simplified it a little... However the results are the same
– Miguel
Right, the
echo
that you did was to present what? Inside theforeach
make aecho $deleteSeveralFile->image_path;
and see what is returned. Just to check if the content of this attribute is valid and what you are expecting...– Adriano Leal
It returns nothing, it’s not even printing an echo 'hey', however it deletes from the printer... I don’t understand...
– Miguel
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
– Miguel
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... =)– Adriano Leal
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.
– brasofilo