基于url变量取消图像链接


Unlink image based on url variable

代码已更新!!我正试图从某个文件夹中删除图像+从数据库中删除数据。到目前为止,它删除了数据库数据,但并没有将图像与数据库数据解除链接。。。脚本片段如下:

          // 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"));

它显示的数组的回声:

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

它正在从数据库中删除数据,但仍然没有取消图像的链接。。。路径似乎还可以,数组也可以。。可能是什么问题?

这里的整个页面代码(如果我把取消链接的功能放错了地方?)

<?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);
?>

您在要删除的文件中使用了相对路径,这可能是的问题

尝试将realpath函数与dirname(__FILE__)__DIR__一起使用以检索文件的绝对路径

如果您有php verion<5.3

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

php版本=>5.3

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

此外,我建议在删除之前检查文件是否存在

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

编辑

你为什么要用这个?

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

代替

$path = $row_rs_galleries['gallery_image'];

我建议你不要把查询字符串放在url上,这段代码有点乱,可能会让代码的调试陷入困境,但无论在哪里,问题都可能出现在数组$row_rs_galleries中,在之前

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

你可以把它放进去看看数组返回的是什么:

echo '<pre>'; var_dump($row_rs_galleries);exit;

现在,您可以看到这个数组中的标识符和值发生了什么D