根据url变量取消链接图像

时间:2014-09-01 16:04:07

标签: php image unlink

代码更新!! 我试图删除某些文件夹中的图像+数据库中的数据。 到目前为止,它删除了数据库数据,但是没有将图像与数据库数据一起取消链接...脚本如下:

          // 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"
}

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

这里的整个页面代码(如果我将unlink功能放在错误的位置?)

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

?>

2 个答案:

答案 0 :(得分:0)

我建议你不要在网址上放置查询字符串,而且这段代码有点混乱,这可能会使代码的调试难堪,但无论在哪里,问题都可能在数组中在

之前的$ row_rs_galleries
$path=$_GET[$row_rs_galleries['gallery_image']];

你可以把它看看数组返回的内容:

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

现在,您可以看到标识符和此数组中的值的内容。 :d

答案 1 :(得分:0)

您正在文件中使用要删除的相对路径,也许这就是问题

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

如果你有php verion&lt; 5.3

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

php version =&gt; 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'];