首页 > 解决方案 > 使用 php 代码从主机中删除 jpg 文件

问题描述

在这个 php 中,我想获取一个“ID”并从数据库中找到该 ID 的 Img1。(Img1 的 url 存储在数据库中)。然后删除这个文件。但 img1 没有删除

function delete(){

$connection = connectToDatabase();
$ID = $_REQUEST['ID'];

if($ID!==""){


    $result1 = mysqli_query($connection,"select Img1 from banners where ID='$ID' ");
    $row = mysqli_fetch_array($result1);
    $namefile1 = $row[0];//(in $namefile1 the url address of img1 is saved like this:http://bestabsd.com/bestfile/pics/jan18-11-28-20-46-36.jpg) 

    $files = glob($namefile1); 
 foreach($files as $file){
if(is_file($file))
unlink($file);}

    mysqli_close($connection);

}

标签: phpserverunlink

解决方案


编辑:我将我的代码编辑为新的以创建路径,但 jpg 尚未删除....

function delete(){

$connection = connectToDatabase();
$ID = $_REQUEST['ID'];

if($ID!==""){


    $result1 = mysqli_query($connection,"select Img1 from banners where ID='$ID' ");
    $row = mysqli_fetch_array($result1);
    $namefile1 = $row[0];
    $namefile11=str_replace("http://bestabsd.com/bestfile/pics/", "", $namefile1);
    $base_directory = '/home/bestabsd/public_html/bestfile/pics/';
    unlink($base_directory.$namefile11);

    mysqli_close($connection);

}else {
    print "null";
}

推荐阅读