首页 文章

从图库中删除图像和复制

提问于
浏览
2

我有一个画廊,我可以上传带有标题和图像简短描述的图像。我将图像存储在我的 ftp 上的文件夹中,将数据存储在数据库中。这是数据库的屏幕截图。

在此输入图像描述

我想让我的客户更多地控制图库,允许他们更新图库并删除图库中的帖子。现在我想专注于 DELETING 部分。

我正在使用以下代码尝试通过 id 和 delete 选择来删除 images/post。

在网站上执行删除脚本时,我在页面或我的 ftp 上没有错误,但图像不会删除。

我正在寻找的最终结果是从表中删除行并从 ftp 中删除图像。

我对 php 很新,知道我需要学习更多关于它的知识,但是如果有人可以提供帮助,我会很感激。我为代码转储道歉,但不知道如何在不显示我正在使用的内容的情况下提出问题。

删除代码:

<?php
//including the database connection file
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");

//getting id of the data from url
$id = isset($_GET['id']) && $_GET['id'] == $row['id'];

//deleting the row from table
$result=mysql_query("DELETE FROM images where id='$id' limit 1;");   

//redirecting to the display page (index.php in our case)
echo '<table align="center" width="100%" height="100%" border="0"><tr align="center" valign="center"><td><h2>Deleting Image</h2></td></tr></table>';
echo '<meta http-equiv="refresh" content="5;URL=/admin/modify-gallery.php">';
?>

这是我用来访问 modify-gallery 页面上的图像的代码

modify-gallery 代码:

include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
/* be safe, not sorry */
foreach ($_REQUEST as $k => $v) {
    $_REQUEST[$k] = mysql_real_escape_string($v);
}
/* take cat from url if exists */
$category = @$_REQUEST["category"] ? $_REQUEST["category"] : null;
$images = mysql_query(
    $category ?
        sprintf(
            "SELECT * FROM images WHERE data_type = '%s'",
            $category
        ) :
        "SELECT * FROM images"
);
if ($images) {
    $total = mysql_num_rows($images);
    if ($total) {
        $per = 12;
        $page = @$_REQUEST["page"] ? $_REQUEST["page"] : 1;
        $pages = ceil($total/$per);
    }
    mysql_free_result($images);
}
?>

然后这用于显示 images/posts 并列出删除和更新 button..(same 页面)

<div class="row">

<ul id="stage" class="portfolio-4column">
<?php
if ($category) {
    $images = mysql_query(sprintf(
        "SELECT * FROM images WHERE data_type = '%s' ORDER BY id DESC LIMIT %d, %d",
        $category, ($page - 1) * $per, $per
    ));
} else $images = mysql_query(sprintf(
    "SELECT * FROM images ORDER BY id DESC LIMIT %d, %d",
    ($page - 1) * $per, $per
));

while ($image=mysql_fetch_array($images))
{
    ?>
<li data-id="id-<?=$image["id"] ?>" data-type="<?=$image["data_type"] ?>">
<div class="grid_3 gallerybox-admin">
<div class="overallheight-admin">
<div class="gallerybox-admin"><a class="fancybox" rel="<?=$image["data_type"] ?>" href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/images/gallery/<?=$image["file_name"] ?>" title="<?=$image["title"] ?>">
<img src="http://<?php echo $_SERVER['SERVER_NAME']; ?>/images/gallery/<?=$image["file_name"] ?>" alt="<?=$image["title"] ?>" class="max-img-border"></a></div>
<div class="galleryh"><?=$image["title"] ?></div>
<div class="galleryp"><?=$image["description"] ?></div>

</div>

<div class="grid_1"><h4 class="btn-green"><a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/admin/delete.php">Delete</a></h4></div>
<div class="grid_1"><h4 class="btn-green"><a href="#">Update</a></h4></div>

</div>

</li>
    <?php
}
?>
</ul>
</div>

来自 Stack Overflow 的代码(当前使用):

<?php
//including the database connection file
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");

//getting id of the data from url
$id = isset($_GET['id']) && $_GET['id'] == $row['id'];

//Select image_name(if not known)
$img = mysql_query("Select file_name from images where id=\"$id\"");
$img_res = mysql_fetch_array($img);
$filename = $img_res[0];

unlink($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $filename);
//deleting the row from table
$result=mysql_query("DELETE FROM images where id=\"$id\" limit 1;");  

//redirecting to the display page
echo '<table align="center" width="100%" height="100%" border="0"><tr align="center" valign="center"><td><h2>Deleting Image</h2></td></tr></table>';
echo '<meta http-equiv="refresh" content="5;URL=/admin/modify-gallery.php">';
?>

3 回答

  • 2

    在删除按钮 html 中修复此问题,以通过 URL 传递文件名

    <h4 class="btn-green"><a href="admin/remove.php?value=<?=$image["file_name"] ?>">Delete</a></h4></div>
    

    在你的 remove.php

    include("/connections/dbconnect.php");
    
    $filename = isset($_GET['value']) ? $_GET['value'] : NULL;
    
    if (!empty($filename)) {
        $delete = unlink("images/gallery/" . $filename);
        if($delete){
            $result = mysql_query("DELETE FROM images where file_name="'.  mysql_real_escape_string($filename)."' limit 1;")";
            header("Location:succes_page.php");
        }else{
            header("Location:failure_page.php");
        }
    }else{
         header("Location:failure_page.php");
    }
    

    旁注尝试将您的 mysql_ *函数更新为 PDO 或 mysqli

  • 1

    “我正在寻找的最终结果是从表中删除行并从 ftp 中删除图像。”

    从表中删除的行✓

    但您仍需要从服务器中删除实际文件才能使用unlink($fileName);

    //getting id of the data from url
    $id = isset($_GET['id']) && $_GET['id'] == $row['id'];
    
    // Delete the file from the server
    unlink($_SERVER['DOCUMENT_ROOT'] . "{Path Where Your Images stored}" . $row['file_name']);
    
    //deleting the row from table
    $result=mysql_query("DELETE FROM images where id='$id' limit 1;");
    

    如您所见,我使用$row['file_name']从您的数据库中获取文件名(很好地向我们展示您的表结构)

  • 1

    要从 ftp 中删除文件,您应该使用

    unlink(filename with complete path);
    

    完整代码://Change 删除以下代码

    <?php
    //including the database connection file
    include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
    
    //getting id of the data from url
    $id = isset($_GET['id']) && $_GET['id'] == $row['id'];
    
    //Select image_name(if not known)
    $img = mysql_query("Select image_name(your column) from images where id=\"$id\"");
    $img_res = mysql_fetch_array($img);
    $filename = $img_res[0];
    
    unlink("path to file".$filename);
    //deleting the row from table
    $result=mysql_query("DELETE FROM images where id=\"$id\" limit 1;");  
    
    //redirecting to the display page (index.php in our case)
    echo '<table align="center" width="100%" height="100%" border="0"><tr align="center" valign="center"><td><h2>Deleting Image</h2></td></tr></table>';
    echo '<meta http-equiv="refresh" content="5;URL=/admin/modify-gallery.php">';
    ?>
    

相关问题