gpt4 book ai didi

php - 删除图像并从图库中复制

转载 作者:行者123 更新时间:2023-11-29 01:41:25 25 4
gpt4 key购买 nike

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

enter image description here

我想通过允许我的客户更新图库和删除图库中的帖子来让他们对图库有更多的控制权。现在我想专注于删除部分。

我正在使用以下代码通过尝试按 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">';
?>

最佳答案

在删除按钮 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

关于php - 删除图像并从图库中复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20592920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com