Deleting Images From an Upload Folder
Posted: Mon Jan 19, 2009 8:22 am
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hi!
I've been working with David Powers PHP Solutions to create a page for uploading images. The images go into an upload folder, image details into the database.
A page to either edit or delete an entry has also been created.
The problem:
Clicking the delete button of a given entry on a page listing various uploaded images takes you to a confirmation page where you are asked if you want to continue with the delete, or cancel the operation.
If the 'DELETE' button is clicked, the images are successfully removed from the upload folder, and corresponding details removed from the database. If the 'CANCEL' button is clicked, the database details are left untouched as they should be, but the images are still removed from the uploads folder.
What I would like to happen is that when 'CANCEL' is clicked the images are left in their folder.
I'm sure I've used unlink() incorrectly and left something out of the code that needs to be there, but I'm not knowledgeable enough to know just where the problem lies.
Here is the code I have been working with.
Thank you!
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hi!
I've been working with David Powers PHP Solutions to create a page for uploading images. The images go into an upload folder, image details into the database.
A page to either edit or delete an entry has also been created.
The problem:
Clicking the delete button of a given entry on a page listing various uploaded images takes you to a confirmation page where you are asked if you want to continue with the delete, or cancel the operation.
If the 'DELETE' button is clicked, the images are successfully removed from the upload folder, and corresponding details removed from the database. If the 'CANCEL' button is clicked, the database details are left untouched as they should be, but the images are still removed from the uploads folder.
What I would like to happen is that when 'CANCEL' is clicked the images are left in their folder.
I'm sure I've used unlink() incorrectly and left something out of the code that needs to be there, but I'm not knowledgeable enough to know just where the problem lies.
Here is the code I have been working with.
Thank you!
Code: Select all
<?php
include($_SERVER['DOCUMENT_ROOT'].'/andychaikin01/connections/conn_mysqli.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/andychaikin01/includes/corefuncs.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/andychaikin01/includes/cms/logout.php');
define ('DELETE_DIR', '/Library/Webserver/Documents/andychaikin01/images/books/main_image/');
define ('THUMBS_DIR', '/Library/Webserver/Documents/andychaikin01/images/books/thumbs/');
// remove backslashes
nukeMagicQuotes();
// create database connection
$conn = dbConnect('admin');
// initialize flags
$OK = false;
$deleted = false;
// get details of selected record
if (isset($_GET['books_id']) && !$_POST) {
// prepare SQL query
$sql = 'SELECT books_id, filename, title
FROM books WHERE books_id = ?';
// initialize statement
$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) {
// bind the query parameters
$stmt->bind_param('i', $_GET['books_id']);
// bind the result to variables
$stmt->bind_result($books_id, $filename, $title);
// execute the query, and fetch the result
$OK = $stmt->execute();
$stmt->fetch();
unlink(DELETE_DIR.$filename);
unlink(THUMBS_DIR.$filename);
}
}
// if confirm deletion button has been clicked, delete record
if (array_key_exists('delete', $_POST)) {
$sql = 'DELETE FROM books WHERE books_id = ?';
$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) {
$stmt->bind_param('i', $_POST['books_id']);
$deleted = $stmt->execute();
}
}
// redirect the page if deletion is successful, cancel button clicked, or $_GET['testimonial_id'] not defined
if ($deleted || array_key_exists('cancel_delete', $_POST) || !isset($_GET['books_id'])) {
include($_SERVER['DOCUMENT_ROOT'].'/andychaikin01/includes/cms/header/books_list.inc.php');
exit;
}
// if any SQL query fails, display error message
if (isset($stmt) && !$OK && !$deleted) {
echo $stmt->error;
}
?>~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: