Link to home
Start Free TrialLog in
Avatar of MariusGM
MariusGM

asked on

PHP Delete record and related image in folder

Dear Experts,

I have a custom built CMS, part of which allows images to be uploaded. I have built my CMS using Dreamweaver. When images are uplaoded, they have an entry inserted in an 'images' table on my DB as well as being uploaded to a folder.

I have a simple 'Delete Record' script that allows images to be deleted from the DB but I need it expanded to delete the actual image in the folder as well.

----------------------------------------------------------------------------------------------------------
The images table is as follows:
----------------------------------------------------------------------------------------------------------
image_ID (pk)
image_desc
image_path
item_ID
----------------------------------------------------------------------------------------------------------
The Delete Script :
----------------------------------------------------------------------------------------------------------
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if ((isset($_GET['img_ID'])) && ($_GET['img_ID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM images WHERE img_ID=%s",
                       GetSQLValueString($_GET['img_ID'], "int"));

  mysql_select_db($database_mphillips, $mphillips);
  $Result1 = mysql_query($deleteSQL, $mphillips) or die(mysql_error());

  $deleteGoTo = "confirm_delImg.php?item_ID=" . $_GET['item_ID'] . "";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}
?>
----------------------------------------------------------------------------------------------------------

I know there is a way, but my knowledge fails me. Any help appreciated.
Avatar of steelseth12
steelseth12
Flag of Cyprus image

This should work.

if ((isset($_GET['img_ID'])) && ($_GET['img_ID'] != "")) {
  $deleteSQL = sprintf("DELETE FROM images WHERE img_ID=%s",
                       GetSQLValueString($_GET['img_ID'], "int"));

  mysql_select_db($database_mphillips, $mphillips);
 
  $get_img_path = mysql_query("SELECT image_path FROM images WHERE img_ID=".GetSQLValueString($_GET['img_ID'], "int"));
 
  list($img_path) = mysql_fetch_row($get_img_path);
 
  unlink($img_path);
 
  $Result1 = mysql_query($deleteSQL, $mphillips) or die(mysql_error());

  $deleteGoTo = "confirm_delImg.php?item_ID=" . $_GET['item_ID'] . "";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}

Note:
Above you state that the primary key is image_ID but in the script you use img_ID, i used img_ID
Avatar of MariusGM
MariusGM

ASKER

Hi steelseth12, that looks perfect. Your right, the pk is img_ID. Typo in the question.

One question I have regarding the unlink of the path. Does the path have to be absolute or relative or what?

As it stands within the DB, paths are stored as (../assets/images/someimage.jpg)
it doesn't matter if its absolute or relative as long as it is accessible from the script.


for the script to work for ../assets/images/someimage.jpg it means that it is located in a directory on the same level as assets.


Sorry buddy, doesn't seem to work. The script is in a folder on the same level. I have ensured the folder permissions are set to 0777. But still no joy.
is the script included from another file ?
No, the page is requested directly through a link i.e. del_img.php?img_ID=123
do you get any errors ?
No errors. The record is deleted and $deleteGoTo is performed. but the file still exists on the server.
comment out the header so we can see the error.

#  header(sprintf("Location: %s", $deleteGoTo));
Ok, I've done what you you have suggested.

del_img.php does not redirect and stays but no error message appears. Source code is blank. Record is deleted, file remains.
ASKER CERTIFIED SOLUTION
Avatar of Jagermonster
Jagermonster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial