php - 当我们从数据库中删除图像时,图像无法从文件夹中删除

标签 php html mysql arrays image

我正在尝试从记录图像的数据库和文件夹中删除图像。这是我的代码。问题是如果我提交删除查询然后数据库图像被删除但文件夹图像仍然存在。请帮忙。

if(empty($_POST)===false)
       {
        if(empty($_POST['events'])===true||empty($_POST['description'])===true||empty($_FILES['images']['name']))
        {
?>                    
                    <div class="alert alert-warning alert-dismissible text-center" role="alert">
                    <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>Add some events and descriptions
                    </div>
 <?php       
        }  
        else
    if (isset($_POST["events"])&&isset($_POST["description"])) 
        {
            $event_title=$_POST["events"];
            $description=$_POST["description"];
            $filetmp=$_FILES["images"]["tmp_name"];
            $filename=$_FILES["images"]["name"];
            $filetype=$_FILES["images"]["type"];
            $filepath= "photo1/".$filename;
            move_uploaded_file( $filetmp,$filepath);
            $data=array($page_id,$event_title,$description,$filepath);
            $add=add_data_event($data);
            if($add)
                {          
                    header('location:hotel1_galery_event.php?page_id=1 && msg=Add Offers Successfully'); 
                }
            foreach ($errors as $value)
                {
                    echo $value;
                }
        }
    }

    if(isset($_GET['id']))
        {
            $id=$_GET['id'];
            delete_event1($id);
            unlink($_GET['file']);
            header('location:hotel1_galery_event.php?page_id=1 && msg=Delete Successfully');
        }
    $data1=get_all_event($page_id);
?>

HTML

 <form action="hotel1_galery_event.php?page_id=1" method="post" class="col-sm-4" enctype="multipart/form-data">
                            <div class="form-group has-info">
                            <label class="control-label" for="inputSuccess">Event title</label>
                            <input type="text" class="form-control" name="events" id="events">
                            </div>                                   
                            <div class="form-group has-info">
                            <label>Event Description</label>
                            <textarea id="description" name="description" placeholder="Event Description" class="form-control " rows="3"></textarea><br><br>
                            </div>
                            <div class="form-group has-info">
                            <label>Event Related images</label>
                            <input type="file" name="images"><br>
                            </div>
                            <button type="submit" class="btn btn-primary">
                            <span>SUBMIT</span>
                            </button> 
                            </form>
                        </div>   
                    </div>  
                </header>
              <div> 
                    <h2 class="text-center"> OUR NEW EVENTS.......</h2>
                </div>
              <div>
                  <table class="table table-striped table-bordered table-hover text-center">
                        <tr class="bg-default">
                            <th>EVENT TITLE</th> 
                            <th>EVENT DESCRIPTION</th> 
                            <th>EVENT RELATED IMAGES</th>
                            <th></th>
                        </tr>
<?php
    foreach ($data1 as $val)
        { 
?>
                        <tr class="bg-default">
                            <td>
<?php  
                            echo "<br>";                                            
                            echo $val['event_title'];

 ?>
                            </td>
                            <td>
<?php  
                            echo "<br>";                 
                            echo $val['event_description'];

 ?>
                            </td>
                            <td>
<?php 
                            echo "<img border=\"0\" src=\"".$val['image_path']."\" width=\"102\"  height=\"91\">";
?>
                            </td>
                            <td>   
                            <a href="hotel1_galery_event.php?page_id=1&&id=<?php echo $val['event_id'];  ?> && msg=Delete Successfully" >
                            <button type="submit" class="btn btn-primary">
                            <span> DELETE</span>
                            </button>
                            </a>&nbsp
                            <a href="hotel1_galery_eventedit.php?page_id=1&&id=<?php echo $val['event_id']; ?> && msg=Edit Successfully" >
                            <button type="submit" class="btn btn-primary">
                            <span> EDIT</span>
                            </button>
                            </a><br>
                            </td>
<?php
        }
?>
                        </tr> 
                    </table>

最佳答案

在您的案例中,有两个选项可以删除图像。

首先

像下面这样在url中发送带有完整路径的文件名

<a href="hotel1_galery_event.php?page_id=1&&id=<?php echo $val['event_id'];  ?> && msg=Delete Successfully &file=<?php echo $filepath ">

其中 $filepath= "photo1/".$filename;

第二

根据id从数据库中获取文件的路径,然后删除它。

关于php - 当我们从数据库中删除图像时,图像无法从文件夹中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33687077/

相关文章:

php - 如何检查 MySQL 查询中是否存在数组中的值

php - 从数据库填充数组

php - 我的代码搜索真的很慢

php - 如何使用 PHP 从 XML "href"标记中提取 "link"属性?

javascript - CSS3 PIE 不适用于 border-radius 和 box-shadow

javascript - 更改为另一张图片时如何固定图片大小?

MySQL 索引有两个相同的列

php - 如何在非面向对象的环境中使用 PHP mysqli()

html - 默认在图片上显示文字

python - sqlalchemy 过滤器