Jump to content

[SOLVED] deleting files with unlink


wrathican

Recommended Posts

hey

im making an image gallery and ive hit a technical hitch with the 'delete album' section

 

i have my code so that the user selects an album to delte and the Id is sent to my processing script.

the script then does the following this order.

1 - sees if there are any images in the images table that belong to that album

-if not it deletes the album from the table

if so:

2 - it gets the names of the files that belong to that album from the db

3 - it deletes (using unlink()) the file from the server

 

but when i process the script every image from all the albums gets deleted.

 

here is my script:

<?php

$alId = $_POST['album'];
$imQuery = "SELECT * FROM won_picture WHERE pic_albumid='".$alId."'";
$imResult = query($imQuery);
$imNumRows = numRows($imResult);

if($imNumRows == 0) {
	//no images connected to the album. delete album
	$alQuery = "DELETE FROM won_album WHERE album_id='".$alId."'";
	$alResult = query($alQuery);

	if($alResult) {
		//album was deleted
		$_SESSION['errorstate'] = 1;
		$errorarray[] = "Thank you. The album you selected has been deleted.";
		$_SESSION['errormessage'] = $errorarray;

		header('location: ../admin/index.php');
	}else{
		//album was not deleted
		$_SESSION['errorstate'] = 1;
		$errorarray[] = "Sorry, the album has not been deleted. Please try again.";
		$_SESSION['errormessage'] = $errorarray;

		header('location: ../admin/galleryadmin.php?func=delalbum');
	}

}else{
	//images are connected to album
	while($row = mysql_fetch_array($imResult)) {
		$id = $row['pic_id'];
		$thumb = $row['pic_thumb'];
		$image = $row['pic_image'];
		$title = $row['pic_title'];

		$thDel = unlink('../images/thumbs/'.$thumb);
		$imDel = unlink('../'.$image);

		if($thDel) {
			//thumb was deleted
			if($imDel) {
				//image was deleted
				$_SESSION['errorstate'] = 1;
				$errorarray[] = "The  image titled ".$title." has been deleted.";
			}else{
				//image was not deleted
				$_SESSION['errorstate'] = 1;
				$errorarray[] = "The  image titled ".$title." has not been deleted, but the thumbnail was.";
			}

		}else{
			//thumb was not deleted
			$_SESSION['errorstate'] = 1;
			$errorarray[] = "The thumbnail image titled ".$title." has not been deleted and neither has the full size image..";
		}
	}

	$_SESSION['errormessage'] = $errorarray;
	header('location: ../admin/galleryadmin.php?func=delalbum');

}

?>

Link to comment
https://forums.phpfreaks.com/topic/115835-solved-deleting-files-with-unlink/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.