新手求助,如何在删除数据库内存储的图片的同时删除对应文件夹下的图片
刚开始学习PHP,网上下了个源码自己研究,有个问题一直搞不明白,如何在删除数据库图片文件的同时,把存在文件夹下的对应的图片一并删除了,研究好几天了,unlink如何使用一直找不到要领,请高手们帮帮忙,不胜感激~~~~~
图片是存储在ytp表中YDT和YXT里的,对应的文件夹是网站根目录2012/b,和2012/s,YDT和YXT字段里存储的是2012/b/1337326878820.jpg和2012/s/1337326878820.jpg
图片存储代码
<?
include("config.inc.php");
include("function.php");
include("in.php");
$conn=mysql_connect($YIHOST,$YIUSER,$YIPASS);
mysql_query("set names gbk");
mysql_select_db($YINAME);
if (!isset($_POST['YLX']) or !isset($_POST['YBH']) or empty($_POST['YFL']))
{
mysql_close();
echo "<script>alert('非法提交!请正确填写后重新提交!');history.back();</script>";
exit;
}
$YTP=yiup($_FILES["YTP"],0,"no.jpg",array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/x-png'),10485760,"tmp/");
if ($YTP=="no.jpg")
{
mysql_close();
echo "<script>alert('上传失败!请注意图片格式和文件大小!');history.back();</script>";
exit;
}
if ($YTP!="no.jpg")
{
$k=rand(100,999);
$YTP0=time().$k.".jpg";
$YTP1=date("Y")."/s/".$YTP0;
$YTP2=date("Y")."/b/".$YTP0;
yisw("tmp/".$YTP,$YTP2,800,600,1);
yisw($YTP2,$YTP1,128,96,0);
yisy($YTP2,"pub/water.gif");
unlink("tmp/".$YTP);
$exec="insert into ytp (YZM,YYH,YLX,YBH,YFL,YXT,YDT) values ('".$yimaoid."','".$YHID."','".htmlencode2($_POST['YLX'])."','".htmlencode2($_POST['YBH'])."','".htmlencode2($_POST['YFL'])."','".$YTP1."','".$YTP2."')";
$result=mysql_query($exec);
}
mysql_close();
echo "<script>alert('上传成功!');history.back();</script>";
exit;
?>
图片删除代码
<?
include("config.inc.php");
include("function.php");
include("in.php");
$conn=mysql_connect($YIHOST,$YIUSER,$YIPASS);
mysql_query("set names gbk");
mysql_select_db($YINAME);
if (empty($_GET['id']))
{
mysql_close();
echo "<script>alert('非法提交!请重新提交!');history.back();</script>";
exit;
}
$YID=htmlencode2($_GET['id']);
$exec="delete from ytp where YYH=$YHID and YID=$YID";
mysql_query($exec);
mysql_close();
echo "<script>alert('删除成功!');history.back();</script>";
exit;
?>
------解决方案--------------------
先删除文件夹中的图片再删除数据库中的数据。
$sql="select YYH,YID from ytp where YYH=$YHID and YID=$YID";
$r=mysql_query($sql);
$row=mysql_fetch_assoc($r);
foreach($row as $v) unlink($v);
$exec="delete from ytp where YYH=$YHID and YID=$YID";
...................................