I've been looking for a solution to this problem for a while now. I've tried many things, and nothing seems to help.
I have a photo upload form on my sister's website which takes an image, resizes it to both thumb size and a size that fits the site's display div, and then creates a database record for the photo based on a name and filename form. In addition, I have pages that list all of the photos in a particular database and allows for editing and deletion of the photos. I've been able to alter and delete database records, but I've still been unable to actually delete the file.
Here is the code, which is an alteration of the code for a deletion form from David Powers' PHP Solutions:
Code: Select all
<?php
include('includes/connection.lizzie.inc.php');
include('includes/corefuncs.php');
define (DELETE_DIR, '/home/owstopit/lizziewrightphotography.com/css/images/photos/wallsanddoors/');
//remove backslashes
nukeMagicQuotes();
//initialize flags
$OK = false;
$done = false;
//create database connection
$conn = dbConnect();
//get details of selected record
if (isset($_GET['photo_id']) && !$_POST) {
//prepare SQL query
$sql = 'SELECT photo_id, photo_name, file_name
FROM wallsanddoors WHERE photo_id = ?';
//initialize statement
$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) {
//bind the query parameters
$stmt->bind_param('i', $_GET['photo_id']);
//bind the results to variables
$stmt->bind_result($photo_id, $photo_name, $file_name);
//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 wallsanddoors WHERE photo_id = ?';
$stmt = $conn->stmt_init();
unlink(DELETE_DIR.$file_name);
if ($stmt->prepare($sql)) {
$stmt->bind_param('i', $_POST['photo_id']);
$deleted = $stmt->execute();
}
}
// redirect the page if deletion is successful, cancel button clicked, or $_GET['article_id'] not defined
if ($deleted || array_key_exists('cancel_delete', $_POST) || !isset($_GET['photo_id'])) {
header('Location: http://www.lizziewrightphotography.com/ ... doors.php');
exit;
}
//display error message if query fails
if (isset($stmt) && !$OK && !$done) {
echo $stmt->error;
}
?>I have tried changing directories to the photo directory then just directly unlinking the filename, but that hasn't worked. Maybe something's up with the $file_name variable? I also tried wrapping the unlink() function in a conditional statement and found that it always failed. Another strange thing is that the failed unlink() function doesn't throw an error... I read that it was supposed to? Anyone know what's up? Also tried changing file permissions for the photo directory (tried every possible combination). Help!
Thanks,
-Neal