Jump to content

DELETE IMAGE(3) FROM DIRECTORY


emediastudios

Recommended Posts

I use the code that dreamweaver produces to delete the record from my sql database.

 

Code below

<?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($_POST['headline'])) && ($_POST['headline'] != "")) {
  $deleteSQL = sprintf("DELETE FROM news WHERE headline=%s",
                       GetSQLValueString($_POST['headline'], "text"));

  mysql_select_db($database_gcproperty, $gcproperty);
  $Result1 = mysql_query($deleteSQL, $gcproperty) or die(mysql_error());

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

mysql_select_db($database_gcproperty, $gcproperty);
$query_Recordset1 = "SELECT * FROM news";
$Recordset1 = mysql_query($query_Recordset1, $gcproperty) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

 

This deletes the record but not the images from the directory, i have to add code to do that and thats where im stuck.

The field in which the file name is displayed is here

 

<input name="photo" type="text" id="photo" value="<?php echo $row_Recordset1['photo']; ?>" />

 

Can i use this reference to delete the image from the ../images dir

 

Thanks 4 any help

 

Link to comment
https://forums.phpfreaks.com/topic/72304-delete-image3-from-directory/
Share on other sites

I am a newbie at this, so im not sure if this will work. I have a photogallery that work similar to yours. The database stores the image location. To delete the files from the directory i do this.

<?php
con;
// Retrieve image and thumbnail path information
$sql = "SELECT photo_location, thumbnail_location FROM photos WHERE photo_id = " . addslashes($_GET['photo_id']);
$result = @mysql_query($sql) or die("Error retrieving records: " . mysql_error());
while ($row = mysql_fetch_array($result)){ 
$photo_location = $row['photo_location'];
$thumb_location = $row['thumbnail_location'];

}
if (@unlink("../adam/photos/" . $photo_location)){
} else {
die("Error deleting photo!<br /><a href=\"index.html\">Please try again</a>.");
}
if (@unlink("../adam/thumbs/" . $thumb_location)){ 
} else {
die("Error deleting thumbnail!<br /><a href=\"index.html\">Please try again</a>.");
} 
?>

 

It basically takes the photo id, fines the location. Then it takes that location and delete that specific photo. As i am a newbie im not sure where to include this in your code. Hope it helps a little

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.