Rodney H.
I just tried out the code that you gave me and yes it works to a point nothing gets deleted but it comes up with all the right information. I modified it so that it works with the file structure that I have. This is what goes wrong when I run the script to delete all the files an error comes up
i checked the directory and the file does exist inside of it
here is teh code
// function to delete category from db, remove the directory and it's contents:
function delete_category2($category_id, $path) {
/* Assumption: "$category_id" is an integer */
// get category name:
$query = "SELECT `category_name` FROM `gallery_category` WHERE `category_id` = ".addslashes((int)$category_id);
if(!$result = mysql_query($query)) {
echo '<p>', mysql_error(), '</p>';
return false;
}
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$directory = $row['category_name'];
$tempDirName = explode(" ", $directory);
$directory = join("_", $tempDirName);
// get files:
$query = "SELECT `photo_id`, `photo_filename` FROM `gallery_photos` WHERE `photo_category` = ".addslashes((int)$category_id);
if(!$result = mysql_query($query)) {
echo '<p>', mysql_error(), '</p>';
return false;
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$photos[$row['photo_id']] = $row['photo_filename'];
}
// if the directory does not exist:
if(!is_dir($path . '/' . $directory)) {
echo '<p>The directory, <em>'.$directory.'<em>, does not exist!</p>';
return false;
}
// unlink each photo and delete from db:
foreach($photos as $key=>$value) {
// unlink both the main image and the thumbnail image
if(unlink($path . '/' . $directory . '/images/' . $value) && unlink($path . '/' . $directory . '/thumbs/tb_' . $value)) {
$query = "DELETE FROM `gallery_photos` WHERE `photo_id` = ".addslashes((int)$key)." LIMIT 1";
if(!$result = mysql_query($query)) {
echo '<p>' . mysql_error() . '</p>';
return false;
}
else $deleted[$key] = $value;
}
}
// if all the images were deleted:
if(count($delted) == count($photos)) {
// if the directory could not be removed:
if(!rmdir($path . '/' . $directory)) {
echo '<p>The directory, <em>'.$directory.'<em>, could not be deleted!</p>';
return false;
}
// else delte the category:
else {
$query = "DELETE FROM `gallery_category` WHERE `category_id` = ". addslashes((int)$category_id) . " LIMIT 1";
if(!$result = mysql_query($query)) {
echo '<p>' . mysql_error() . '</p>';
return false;
}
else {
return true;
}
}
}
// if not all of the images were deleted:
else {
echo '<p>Not all of the photos have been deleted!</p>';
return false;
}
}
sry about any delayed responses from me college is a little crazy in this website I'm making is just a hobby i'm not even a CS major.