从服务器和数据库中删除视频

时间:2019-04-04 11:50:21

标签: php filezilla

我有一个博客,您还可以在其中添加图像和视频。我的问题是,当我删除博客时,图像或视频仍将保留在服务器上,并且服务器上的文件过多对服务器不利。经过一番研究,我发现了unlink()函数。所以我尝试了一下,但是没有用。图像不会从服务器中删除。它只会删除它们的数据库。

我尝试过的事情

if(isset($_POST['deleteSubcat'])){
  $del = $_POST['deleteSubcat'];
    DeleteSubCat($del);
}

function DeleteSubCat($del){
    include '../../../include/dbConnection/dbcon.php';

    //to skip the update query because the item is about to delete.
    $type = '1';
    // DeleteVideo($del, $type);

    //deletes subcat from tables hw_subcategorie.
    $sqldelete = 'DELETE FROM hw_subcategorie WHERE subcategorie_id = "'.$del.'"';
    //if query is done right then 'Record updated successfully'
    if (mysqli_query($conn, $sqldelete)) {
      $sqlimg = 'SELECT video_link FROM hw_subcategorie WHERE subcategorie_id = "'.$del.'"';
      $result = mysqli_query($conn,$sqlimg);
      $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
      unlink("video/".$row["video_link"]);
        echo "Record deleted successfully";
      } else {
        echo "Error deleted record: " . mysqli_error($conn);
    }
  }

2 个答案:

答案 0 :(得分:0)

您正在尝试搜索已删除的记录,以从服务器中删除视频。

在删除之前执行选择:

function DeleteSubCat($del){
    include '../../../include/dbConnection/dbcon.php';

    //to skip the update query because the item is about to delete.
    $type = '1';

    // Get the video link before delete it from db
    $sqlimg = 'SELECT video_link FROM hw_subcategorie WHERE subcategorie_id = "'.$del.'"';
    $result = mysqli_query($conn,$sqlimg);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $videoLink = $row["video_link"];
    unlink("video/".$videoLink);
    //deletes subcat from tables hw_subcategorie.
    $sqldelete = 'DELETE FROM hw_subcategorie WHERE subcategorie_id = "'.$del.'"';
    //if query is done right then 'Record updated successfully'
    if (mysqli_query($conn, $sqldelete)) {
        echo "Record deleted successfully";
    } else {
        echo "Error deleted record: " . mysqli_error($conn);
    }
}

答案 1 :(得分:-2)

希望这对您有所帮助。

//Differently Formatted Select
$resdb = $db->query("

SELECT video_link
FROM hw_subcategorie
WHERE subcategorie_id = '$del'
Order by subcategorie_id
");

?>


<?php

 if($resdb->num_rows) {
 while($r = $resdb->fetch_object()){
 //Deleting from server
 $deletevid = 'video/'.$r->video_link; unlink($deletevid);
 //Deleting from Database
 $conn = new mysqli($servername, $username, $password, $dbname);
 $sql="DELETE FROM hw_subcategorie WHERE subcategorie_id = '$del'";
 if ($conn->query($sql) === TRUE) {
 }
}
}
?>

`