Page 1 of 1

Deleting Images From an Upload Folder

Posted: Mon Jan 19, 2009 8:22 am
by johnedwards
~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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Deleting Images From an Upload Folder

Posted: Mon Jan 19, 2009 9:00 am
by mattpointblank
Why give them a cancel button at all? Just give a confirm deletion button, and a text link back somewhere 'safe'?

Re: Deleting Images From an Upload Folder

Posted: Mon Jan 19, 2009 9:28 am
by johnedwards
The problem is that when the delete button is clicked the first time, the image in the upload folder is removed.

After clicking delete initially, you are shunted to a confirmation page and given the option to continue with the delete, or abort.

The cancel/abort button on the confirmation page is a text link, taking you back safely to the page listing the images, where you should see that your images are still in their folder. But the images are already gone from the thumbs and mainImage folder.

unlink() is working, but it's working prematurely.

Re: Deleting Images From an Upload Folder

Posted: Mon Jan 19, 2009 10:08 am
by mattpointblank
Ah, thanks for clarifying.

The problem seems to be when the button's pressed - how is the $_GET['books_id'] variable being set? As soon as its set, your files are deleted. On the other hand, you're only deleting the database record when array_key_exists('delete', $_POST) is set. There's your problem.

Re: Deleting Images From an Upload Folder

Posted: Mon Jan 19, 2009 11:22 am
by pickle
$_POST will always evaluate to TRUE - even if a form hasn't been submitted. So it's not useful as a condition.

Move your unlink() calls down to where you delete the database records.

Re: Deleting Images From an Upload Folder

Posted: Mon Jan 19, 2009 4:43 pm
by johnedwards
Thank you Matt
Thank you Briney

I moved the unlink() calls to the section where 'I think' the database records are being deleted as seen on line 39 -40

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();
    }   
  }
// 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();
    unlink(DELETE_DIR.$filename);
    unlink(THUMBS_DIR.$filename);
    }
  }  
// 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;
  }
?>
 
The result was 4 errors. The details in the DB were deleted, but the images remained in their upload folders.

Here are the errors that were returned when delete was clicked.

Code: Select all

 
 
Notice: Undefined variable: filename in /Library/WebServer/Documents/andychaikin01/cms/bookpage/books_delete.php on line 28
 
Warning: unlink(/Library/Webserver/Documents/andychaikin01/images/books/main_image/) [function.unlink]: Permission denied in /Library/WebServer/Documents/andychaikin01/cms/bookpage/books_delete.php on line 28
 
Notice: Undefined variable: filename in /Library/WebServer/Documents/andychaikin01/cms/bookpage/books_delete.php on line 29
 
Warning: unlink(/Library/Webserver/Documents/andychaikin01/images/books/thumbs/) [function.unlink]: Permission denied in /Library/WebServer/Documents/andychaikin01/cms/bookpage/books_delete.php on line 29
 
Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/andychaikin01/cms/bookpage/books_delete.php:28) in /Library/WebServer/Documents/andychaikin01/includes/cms/header/books_list.inc.php on line 1
 
Where should $filename be defined?

Thank you for being patient with a new forum member, and an inexperienced but eager to learn coder.

Re: Deleting Images From an Upload Folder

Posted: Tue Jan 20, 2009 3:26 am
by mattpointblank
Your select query that gets the data isn't happening in the code area that's being executed when they click the delete button. Maybe store the filename detail etc in a session? Or just do the select query again before deleting the files?