从图库中删除图像和复制

时间:2013-12-15 09:06:08

标签: php mysql sql-delete delete-row

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

enter image description here

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

我正在使用以下代码尝试通过ID和删除来尝试删除图片/帖子。

在网站上执行删除脚本时,我在页面或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">';
?>

这是我用来访问修改图库页面上的图像的代码

修改图库代码:

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

然后用于显示图像/帖子并列出删除和更新按钮..(同页)

<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 个答案:

答案 0 :(得分:2)

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

<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 :(得分:1)

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

unlink(filename with complete path);

完整代码: //将删除代码更改为以下

<?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">';
?>

答案 2 :(得分: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']从您的数据库中获取文件名(很好地向我们展示您的表格结构)

相关问题