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("GetSQLV alueString ")) {
function GetSQLValueString($theValu e, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_rea l_escape_s tring") ? mysql_real_escape_string($ theValue) : mysql_escape_string($theVa lue);
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['i mg_ID'], "int"));
mysql_select_db($database_ mphillips, $mphillips);
$Result1 = mysql_query($deleteSQL, $mphillips) or die(mysql_error());
$deleteGoTo = "confirm_delImg.php?item_I D=" . $_GET['item_ID'] . "";
if (isset($_SERVER['QUERY_STR ING'])) {
$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.
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("GetSQLV
function GetSQLValueString($theValu
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_rea
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['i
mysql_select_db($database_
$Result1 = mysql_query($deleteSQL, $mphillips) or die(mysql_error());
$deleteGoTo = "confirm_delImg.php?item_I
if (isset($_SERVER['QUERY_STR
$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.
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/someimag e.jpg)
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/someimag
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.
for the script to work for ../assets/images/someimage
ASKER
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 ?
ASKER
No, the page is requested directly through a link i.e. del_img.php?img_ID=123
do you get any errors ?
ASKER
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));
# header(sprintf("Location: %s", $deleteGoTo));
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if ((isset($_GET['img_ID'])) && ($_GET['img_ID'] != "")) {
$deleteSQL = sprintf("DELETE FROM images WHERE img_ID=%s",
GetSQLValueString($_GET['i
mysql_select_db($database_
$get_img_path = mysql_query("SELECT image_path FROM images WHERE img_ID=".GetSQLValueString
list($img_path) = mysql_fetch_row($get_img_p
unlink($img_path);
$Result1 = mysql_query($deleteSQL, $mphillips) or die(mysql_error());
$deleteGoTo = "confirm_delImg.php?item_I
if (isset($_SERVER['QUERY_STR
$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