Jump to content

DELETING the associated files


student101

Recommended Posts

I'll try n explain my issue, looking for ideas to understand more of what it is that I am trying to achieve.

Two tables for imagesalbum_alb <- holds main albumalbum_img <- holds linked data related to album_alb
I think I'm trying a recursive delete, not sure?Essentially, If I delete from the album page (may have linked data from album_img) it needs to loop through the specified folder(s) to delete the specific file(s).What always works is the DELETE from MySQL, the part that doesn't work is DELETING ALL the associated files. Does this even make sense?
Link to comment
Share on other sites

Wait, so album_img contains paths to images, and you want to not only delete the image paths, but the images themselves?It's best to do that from an admin panel in which PHP will select the data before deleting it, and after a successful delete from the DB, it does the deletion of the files pointed by the previously selected data.The alternative is to create a stored procedure that does just that - delete the data, but return the deleted data as a result set for PHP to then delete the files.The third option (most efficient, but more wasteful of space on the host) is to create a separate file deletion script that you'd execute as a "cron job" i.e. at regular intervals. The script will loop over all image files, and delete anyone that's not in the DB.I don't think MySQL itself can delete files.

Link to comment
Share on other sites

Yeah thanks..It's from an admin panel, but doing it piece by piece...First - Delete the file then delete the data from MySQL.Looking for a better way of processing this:

// CONNECT TO DB..//get URL var..if ((isset($_GET['id_alb'])) && ($_GET['id_alb'] != "")) {// enable the delete function..function file_delete($file){ chmod($file, 0777); if(unlink($file)){ return true; }else{ return false; } }// assign var..$pid = $_GET['id_alb'];// do select..mysql_select_db($database_cncath, $cncath);$query_item = "SELECT id_alb, thumb, imagename FROM album_alb WHERE id_alb=".$pid or die(mysql_error());$item = mysql_query($query_item, $cncath) or die(mysql_error());$row_item = mysql_fetch_assoc($item);$totalRows_item = mysql_num_rows($item);// start loop..while($totalRows_item = mysql_fetch_assoc($item))  {// check if empty before delete..if(!empty($row_item['thumb']) || !empty($row_item['imagename'])) {// assign more vars..$thmimage = "../uploads/thm/".$row_item['thumb'];$imagefield = "../uploads/lrg/".$row_item['imagename'];// check if file even exists before delete..if(file_exists($thmimage)){file_delete($thmimage);}if(file_exists($imagefield)){file_delete($imagefield);}}} // end loop..// Process working DELETE from MySQL..

Link to comment
Share on other sites

Resolved!Thanks for your replies!Created a "checking" page with a select statement to see if any related data exists then offer the obvious options of deleting the linked data or the album.A little silly :) actually - my head is clouded forgive me for this trivial question!.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...