eve_mf eve_mf - 23 days ago 6
PHP Question

removing image from folder after path is removed from db table through mysql trigger

I am still working in my recipe book, at this point, I am trying to remove the image for the recipe if this gets removed.

My database has a table for recipes, with columns "id", "name" and "attachment_id". Also a table called attachments with "id" and "path_to_attachment" columns. (I understand the useless point of having this in two tables, I just need to practise moving around databases and interacting with them).

After lots of messing around yesterday, I added a trigger for this attachment_id, so, when a recipe gets removed, the row for its attachment and path is also removed.

The problem? The image reminds in the "images/" folder. I am now trying to remove the image when the recipe and its attachments are removed from the database...

The image is stored on this folder with this, on recipe.php:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$attachment_id = filter_input(INPUT_POST, 'attachment_id', FILTER_SANITIZE_NUMBER_INT);

$folder="images/";
$file = $_FILES['photo']['tmp_name'];
$file_to_upload = $folder . basename($_FILES['photo']['name']);

if(move_uploaded_file($file, $file_to_upload)) {
echo "File is valid, and was successfully uploaded.\n";


if($attachment_id = add_image($file_to_upload)) {
if(add_recipe($name, $attachment_id)) {
header('Location: index.php');
exit;
} else {
$error_message = "Could not add recipe";
}
} else {
$error_message = "Could not add image";
}

} else {
echo 'Upload failure';
}
}


Then, on index.php, I get the attachments and recipes, and I have a button next to each recipe for it to be deleted:

$recipes = get_recipes()
$attachments = get_attachments();
$attachment_path = find_path_by_id($recipe['attachment_id']);

<a class="toLink" href="delete_recipe.php?id=' . $recipe['id'] . '" title="delete recipe" onclick="return confirm('Are you sure you want to delete this recipe?');">Delete recipe</a>


On delete_recipe.php:



$recipeId = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$attachment_path ?????
if(delete_recipe($recipeId) == true) {
delete_attachment($attachment_path);
echo $attachment_path; die();
header('Location: index.php');
exit;
} else {
$error_message = "Could not delete recipe";
}


On fuctions.php:

function get_recipes() {
include'db_connection.php';
try {
return $conn->query("SELECT * FROM recipes");

} catch (PDOException $e) {
echo 'Error:' . $e->getMessage() . "<br />";
return array();
}
return true;
}

function get_attachments() {
include'db_connection.php';
try {
return $conn->query("SELECT * FROM attachments");

} catch (PDOException $e) {
echo 'Error:' . $e->getMessage() . "<br />";
return array();
}
return true;
}

function find_path_by_id($attachment_id = ':attachment_id') {
include 'db_connection.php';

$sql = 'SELECT * FROM attachments WHERE id=:attachment_id LIMIT 1';

try {
$results = $conn->prepare($sql);
$results->bindParam(':attachment_id', $attachment_id, PDO::PARAM_INT);
$results->execute();
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage() . '<br />';
return array();
}
return $results->fetch(PDO::FETCH_ASSOC);
}

function add_image($attachment_path= ':attachment_path') {
include 'db_connection.php';
try {
$sql = "INSERT INTO attachments(attachment_path) VALUES (:attachment_path)";

$results = $conn->prepare($sql);
$results->bindParam(':attachment_path', $attachment_path, PDO::PARAM_STR, 100);
$results->execute();
$id = $conn->lastInsertId();

$conn = null;

} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage() . '<br />';
return false;
}
return $id;
}

function display_image($attachment_id = ':attachment_id') {
include 'db_connection.php';

$sql = 'SELECT * FROM attachments WHERE id=:attachment_id LIMIT 1';

try {
$results = $conn->prepare($sql);
$results->bindParam(':attachment_id', $attachment_id, PDO::PARAM_INT);
$results->execute();
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage() . '<br />';
return array();
}
return $results->fetch(PDO::FETCH_ASSOC);
}


The problem is, I can't get the attachment_path once I clicked delete recipe. I have tried to add in but I don't want to send it on the url, and I can't get the path once it is processing the "delete recipe".

My thoughts were,once the recipe gets deleted, I create a function to find the name of the file inside the images directory and then, I delete it, but, as I said, I don't know how can I pass the image name to the delete_recipe.php file.

I guess it must be a more logical way of doing this.... but I don't know how... any suggestions?

Thank you!

Answer

Use unlink function for delete image from folder unlink($attachment_path), attachment path should be for example images/abc.jpg

// image path return from delete_recipe() function
$attachment_path = delete_recipe($recipeId);
if(isset($attachment_path)) {
  unlink($attachment_path);
  header('Location: index.php');
} else {
   $error_message = "Could not delete recipe";
}