dqsong2010 2016-08-21 21:45
浏览 53

从目录中删除特定文件,并在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>';
  • 写回答

1条回答 默认 最新

  • doushu8260 2016-08-21 21:59
    关注

    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');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了