Php 通过数据库和目录删除图像

Php 通过数据库和目录删除图像,php,html,mysql,Php,Html,Mysql,谁能帮我查一下密码吗。我可以删除数据库中的图像,但无法删除目录中的图像。我试了很长时间,但似乎根本不起作用。谁能帮帮我吗?这是我的代码:这是图像所在的代码 <? //this is were images displayed $query = "SELECT * FROM images WHERE category='home'"; $result = mysql_query($query) or die(my

谁能帮我查一下密码吗。我可以删除数据库中的图像,但无法删除目录中的图像。我试了很长时间,但似乎根本不起作用。谁能帮帮我吗?这是我的代码:这是图像所在的代码

<?
//this is were images displayed
                    $query = "SELECT * FROM images WHERE category='home'";
                    $result = mysql_query($query) or die(mysql_error());
                    while($row = mysql_fetch_array($result)){
                    ?>
                    <a href="edithomephotos.php?delete=<?=$row['imageID']?>" onclick = "return confirm('Are you sure you want to delete?')"><img src="images/template/delete.png" id="AEDbutton"></a>


                    echo "<img border=\"0\" src=\"".$row['image']."\" width=\"200\"  height=\"100\">";
                    echo "<br>";                }   


?>

您已经删除了表中的图像条目,然后尝试在DB中获取相同的条目。因此,首先可以从文件夹中删除图像,然后可以在表中删除图像

<?php
    include('global.php');

    if($delete != "") {
        $query = "SELECT * FROM images WHERE imageID='".$delete."'";
        $result = mysql_query($query);

        while ($delete = mysql_fetch_array($result)) {
            $image = $delete['image'];
            $file= '/.directory/'.$image;
            unlink($file);
        }

        $query = "DELETE FROM images WHERE imageID='".$delete."'";
        ExecuteQuery($query);   
    }
?>

您已经删除了表中的图像条目,然后尝试在DB中获取相同的条目。因此,首先可以从文件夹中删除图像,然后可以在表中删除图像

<?php
    include('global.php');

    if($delete != "") {
        $query = "SELECT * FROM images WHERE imageID='".$delete."'";
        $result = mysql_query($query);

        while ($delete = mysql_fetch_array($result)) {
            $image = $delete['image'];
            $file= '/.directory/'.$image;
            unlink($file);
        }

        $query = "DELETE FROM images WHERE imageID='".$delete."'";
        ExecuteQuery($query);   
    }
?>

好笑。这是因为您首先从数据库中删除图像id,然后尝试获取以前删除的图像(不再存在)的id并删除与其关联的文件。像这样切换代码

include('global.php');

if($delete != "") 
{
//first delete the file
$query = "SELECT * FROM images WHERE imageID='".$delete."'";

$result = mysql_query($query);

while ($delete = mysql_fetch_array($result))
{
    try
    {
        $image = $delete['image'];
        $file= '/images/'.$image;
        unlink($file);
    } catch (Exception $e) {

    }
}

// after that delete the id from the db of that image associated with the deleted file
$query = "DELETE FROM images WHERE imageID='".$delete."'";
ExecuteQuery($query);   
}

更新:我添加了一个try-catch

搞笑。这是因为您首先从数据库中删除图像id,然后尝试获取以前删除的图像(不再存在)的id并删除与其关联的文件。像这样切换代码

include('global.php');

if($delete != "") 
{
//first delete the file
$query = "SELECT * FROM images WHERE imageID='".$delete."'";

$result = mysql_query($query);

while ($delete = mysql_fetch_array($result))
{
    try
    {
        $image = $delete['image'];
        $file= '/images/'.$image;
        unlink($file);
    } catch (Exception $e) {

    }
}

// after that delete the id from the db of that image associated with the deleted file
$query = "DELETE FROM images WHERE imageID='".$delete."'";
ExecuteQuery($query);   
}

更新:我添加了一个try-catch

如果您要删除图像,首先需要删除图像,然后在DB中删除如果您要删除图像,首先需要删除图像,然后在DB中删除您应该在第一个$delete处替换为$\u请求['delete']您是否意识到在删除记录后执行了一个
SELECT
语句?这正是我使用galleryproject[用于后端的原因是,我确实有请求。我刚才更改了它,但错误是“警告:取消链接(../directory/directory/33.jpg)[function.unlink]:第27行的C:\wamp\www\updatedTHESIS\updatedlamadmin\edithomephotos.php中没有这样的文件或目录“而且我不知道为什么你应该首先用$_REQUEST['delete']替换$delete。你知道你在
删除记录之后执行了
选择
语句吗?这正是我使用galleryproject的原因[对于我的后端工作人员是的,我确实有请求。我刚才更改了它,但错误是“警告:取消链接(../directory/directory/33.jpg)[function.unlink]:在第27行的C:\wamp\www\updatedTHESIS\updatedlamadmin\edithomephotos.php中没有这样的文件或目录”我不知道为什么我以前试过,但它是错误的。错误是警告:取消链接(/.directory/directory/33.jpg)[function.unlink]:第27行的C:\wamp\www\updatedTHESIS\updatedlamadmin\edithomephotos.php中没有这样的文件或目录这意味着您试图删除的映像33.jpg不存在,我将更新代码我以前尝试过它,但它是错误的。错误是警告:取消链接(/.directory/directory/33.jpg)[function.unlink:第27行的C:\wamp\www\updatedTHESIS\updatedlamadmin\edithomephotos.php中没有这样的文件或目录这意味着您试图删除的映像33.jpg不存在,我将更新代码我尝试了这个“$file=“../directory/”$image.”,但似乎没有任何效果。错误是:警告:取消链接(/.directory/directory/33.jpg)[function.unlink]:第27行的C:\wamp\www\updatedTHESIS\updatedialamadmin\edithomephotos.php中没有这样的文件或目录。请确保将其指向正确的文件夹。您可能不需要将
'/.directory/'
添加到
$file
-变量中。请仅尝试使用
unlink($file)
并查看您得到了什么。如果您的图像存储在特定的文件夹中,比如说当前工作目录中的“/images/”,您可以使用
取消链接('images/”。$image)
。我尝试了这个“$file=“../directory/”$image.”,但似乎没有任何效果。错误是:警告:取消链接(/.directory/directory/33.jpg)[function.unlink]:第27行的C:\wamp\www\updatedTHESIS\updatedialamadmin\edithomephotos.php中没有这样的文件或目录。请确保将其指向正确的文件夹。您可能不需要将
'/.directory/'
添加到
$file
-变量中。请仅尝试使用
unlink($file);
并查看您得到了什么。如果您的图像存储在特定的文件夹中,比如说当前工作目录中的“/images/”,您可以使用
取消链接('images/。$image)