Search code examples
phpimageunlink

Unlink image based on url variable


code updated!! I am trying to delete image from certain folder + data from database. so far it deletes database data, however it is no unlink'ing the image together with database data...script piece as follows :

          // echo '<pre>'; var_dump($row_rs_galleries);exit;
    $path=$_get[$row_rs_galleries['gallery_image']];
    $file="../uploads/gallerytitle/resized/$path";
    unlink($file);

$deleteSQL = sprintf("DELETE FROM galleries WHERE gallery_id=%s",
                       GetSQLValueString($_GET['gid'], "int"));

echoing out array it shows:

array(4) {
  ["gallery_id"]=>
  string(2) "20"
  ["gallery_name"]=>
  string(9) "sdvsdvsdv"
  ["gallery_image"]=>
  string(27) "resized_140959609221730.JPG"
  ["image_alt"]=>
  string(8) "dsvsdvsd"
}

It is deleting data from database but still not unlinking the image...Path seems to be ok, Array as well..What could be the problem?

The whole page code here (in case i am placing unlink function in a wrong place?)

<?php require_once('../Connections/conn_hell.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $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;
}
}
$colname_rs_galleries = "-1";
if (isset($_GET['gid'])) {
  $colname_rs_galleries = $_GET['gid'];
}
mysql_select_db($database_conn_hell, $conn_hell);
$query_rs_galleries = sprintf("SELECT * FROM galleries WHERE gallery_id = %s", GetSQLValueString($colname_rs_galleries, "int"));
$rs_galleries = mysql_query($query_rs_galleries, $conn_hell) or die(mysql_error());
$row_rs_galleries = mysql_fetch_assoc($rs_galleries);
$totalRows_rs_galleries = mysql_num_rows($rs_galleries);
if ((isset($_GET['gid'])) && ($_GET['gid'] != "")) {
    //--------------------------The unlink----------------------------------------------------------
//echo '<pre>'; var_dump($row_rs_galleries);exit;

    $path=$_GET[$row_rs_galleries['gallery_image']];
    $file= realpath(__DIR__ . "../uploads/gallerytitle/resized/$path");
     unlink($file);
 //-----------------------------------------------End unlink

$deleteSQL = sprintf("DELETE FROM galleries WHERE gallery_id=%s",
                       GetSQLValueString($_GET['gid'], "int"));

mysql_select_db($database_conn_hell, $conn_hell);

  $Result1 = mysql_query($deleteSQL, $conn_hell) or die(mysql_error());

  $deleteGoTo = "../announcments.php?aid=5";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}
 ?>
<?php
mysql_free_result($rs_galleries);

?>

Solution

  • You are using a relative path in the file that you want to delete, maybe that is the problem

    Try using realpath function with dirname(__FILE__) or __DIR__ to retrieve the absolute path of the file

    If you have php verion < 5.3

    $file= realpath(dirname(__FILE__) . "../uploads/gallerytitle/resized/$path");
    

    php version => 5.3

    $file= realpath(__DIR__ . "../uploads/gallerytitle/resized/$path");
    

    Also I recommend to check if the file exists before delete it

    if (file_exists($file)) {
        unlink($file);
    }
    

    EDIT

    Why are you using this?

    $path = $_GET[$row_rs_galleries['gallery_image']];
    

    Instead of

    $path = $row_rs_galleries['gallery_image'];