从php文件夹中删除图像

时间:2016-04-12 06:44:17

标签: php

<?php
include ('config.php');

echo '<script>confirm("Are you sure?");</script>';

if(isset($_GET['fileid']))
{
    $fileid = $_GET['fileid'];
    $target_dir = "img/";
    $target_file =  $target_dir .$_GET['fileid'];
    $query = "DELETE FROM file_info WHERE fileid='$fileid'";
    $result = mysqli_query($conn,$query);
    $row = mysqli_fetch_array($result);
    $path = "img/".$row['filename'];

    if($result)
    {
         unlink($path);
         echo'<script>window.location ="table.php";</script>';
     }
    else
    {
         echo'<script>window.location ="table.php";</script>';
    }
 }
 ?>

我不知道为什么它不起作用。

此代码正在更新数据库但不删除文件夹中的文件需要将其删除。

3 个答案:

答案 0 :(得分:0)

使用select query使用fileid获取文件名。然后执行delete statement。

<?php
 $path = "img/".$row['filename'];
if( file_exists ( $path ))
    unlink( $path);
else
 echo "file not found";

 ?>

答案 1 :(得分:0)

打印$ result&amp;交叉检查你是否能够到达if()块内部。同样从中获取值$row['filename']要获取它,首先使用选择查询选择值。

答案 2 :(得分:0)

更改删除查询以选择并为$row['filename']

提供绝对路径和获取关联数组

<强> CODE

$fileid = $_GET['fileid'];
$target_dir = "img/";
$target_file =  $target_dir .$_GET['fileid'];
$query = "SELECT filename FROM file_info WHERE fileid='$fileid'";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_assoc($result); // And fetch assoc array

$path = "c:\project\img\".$row['filename'];