从目录中删除特定文件,并在foreach循环中单击按钮时从数据库数组中删除其名称

How can I delete the files from array in database, and from directory on button click. I have this code which has deleted all the files in that folder but still showing the file names. I am looking to delete specific file from directory folder and array in database on button click.

echo '<ul>';

foreach (explode(',', $msg_rows['msg_files']) as $file){

echo '<li><a href="../files/wcfiles/'.$msg_rows['msg_order_id'].'/'.$file.'">'.$file.'</a>&nbsp
<a class="btn btn-danger btn-xs" href="../files/wcfiles/'.$msg_rows['msg_order_id'].'/'.unlink($file).'">Delete</a></li>';}

echo '</ul>';

To delete all the files you get by processing $msg_rows['files'] you could do something like this:

$files=explode(',', $msg_rows['files'] );
foreach ( $files as $file ) @unlink($file);

If you are trying to generate a list of files with a link that, when clicked, will delete the file that is a little more involved - most likely would be to use ajax.

$files=explode(',', $msg_rows['files'] );
echo '<ul>';
foreach ( $files as $file ) echo "<li>$file<a href='#' onclick='deletefile(\"{$file}\")'>delete</a></li>";
echo '</ul>';

You would then need to have a javascript function deletefile that would send the necessary information to a script that deleted the file. This is a very basic example ( embedding the filename as a parameter )

/* javascript */
function deletefile(filename){
    var xhr=new XMLHttpRequest();
    xhr.onreadystatechange=function(){
        if( xhr.readyState==4 && xhr.status==200 ) {
            alert( xhr.response );
        }
    }
    xhr.open( "POST", "/deletefile.php", true )
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xhr.send("file="+filename+'action='delete');
}