简体   繁体   中英

Deleting files from server using PHP

My server stores 2 video files and 2 database records.

I am trying to delete it from my database if user chooses to delete the files.

Script to delete from database:

$result = mysqli_query($db_connection , "DELETE FROM `viewvideo` WHERE `video_id`='".$video_id."'");
$result2 = mysqli_query($db_connection , "DELETE FROM `video480p` WHERE `video_id`='".$video_id."'");

Using this script to retrieve the file path:

$deleteOrigVideo = mysqli_query($db_connection, "Select video_link FROM viewvideo WHERE `video_id`='".$video_id."'");
$delete480pVideo = mysqli_query($db_connection, "Select video_link FROM video480p WHERE `video_id`='".$video_id."'");

Delete Files:

unlink($deleteOrigVideo);
unlink($delete480pVideo);

The unlink part is not working. I am getting unlink() expects parameter 1 to be a valid path,

Original video is stored in original and 480p video is store in original\\480p

var_dump($deleteOrigVideo, $delete480pVideo);

Output:

   object(mysqli_result)[2]
      public 'current_field' => null
      public 'field_count' => null
      public 'lengths' => null
      public 'num_rows' => null
      public 'type' => null
    object(mysqli_result)[3]
      public 'current_field' => null
      public 'field_count' => null
      public 'lengths' => null
      public 'num_rows' => null
      public 'type' => null

$deleteOrigVideo and $delete480pVideo are not strings; They are MySQLi query result objects .

You'll want to do something like this (repeat for the second video):

if($deleteOrigVideo) {
    while ($videoEntry = $deleteOrigVideo->fetch_assoc()) {
        unlink($videoEntry['video_link']);
    }
} 

I'm assuming the paths returned by "$videoEntry['video_link']" point where you want them to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM