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

时间:2015-11-13 06:29:11

标签: 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>

1 个答案:

答案 0 :(得分:0)

在您的案例中有两种删除图像的选项。

第一

在网址中发送带有完整路径的文件名,如下所示

<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从数据库中获取文件路径,然后将其删除。