PHP ~ 通过程序删除图片,同时删除数据库中的图片数据 和 图片文件

删除单张图片

<?php

require_once '../../conn.php'; //连接数据库

$ID = $_GET['ID'];         //获取删除按钮传入要删除图片的 ID

$sql = "select * from blogalbum where ID = {$ID}";

$results = mysql_query($sql);

$blog = mysql_fetch_assoc($results);   //将查询结果存储到 $blog中

  /*

  * $blog['pic'] 为 数据库中存储的图片的路径

  * unlink(path) unlink根据path删除图片

  */

unlink('../.'.$blog['pic']);        // 删除图片文件成功

mysql_free_result($results);      // 释放结果集

  /*

  * 下面的就像执行数据库删除图片信息的操作

  */

if ( isset($_GET['ID']) && !empty($_GET['ID']) ) {

$ID = $_GET['ID'];

}else{

echo '获取不到id';

header('../admin/album.php');

}

$sql = "delete from blogalbum where ;

$result = mysql_query($sql);

if ($result == true) {

echo <<<a

<script type="text/javascript">

window.location.href = '../../admin/album.php';

</script>

a;

}else{

echo <<<a

<script type="text/javascript">

alert('删除失败');

window.location.href = '../../admin/album.php';

</script>

a;

}

?>