从 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>';
    }
 }
 ?>

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

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

使用选择查询 使用 fileid 获取文件名。 然后执行删除语句。

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

打印$result并交叉检查您是否能够到达if((块内部。同样从获取值的位置$row['filename'] 若要获取它,请先使用选择查询选择值。

更改删除查询以选择和提供绝对路径并获取$row['filename']的关联数组

法典

$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:projectimg".$row['filename'];

最新更新