drkug66408 2017-05-27 20:42
浏览 10
已采纳

澄清 - 删除不在mySQL TABLE中的文件

Delete files which are not in a mySQL TABLE

The link above is to a Stack Overflow question with an answer that is (I believe) pretty close to what I'm looking for. I'm actually just seeking further clarification on the answer given.


The Question

I'm trying to delete files (picture files) in a folder only if they're not present in a specific database table.

Just like a check of filenames and if they're present in the table it's ok but if not delete them.

Any ideas how to do that?

The accepted answer

$result = mysql_query("SELECT filename FROM no_delete"); 
while($row = mysql_fetch_assoc($result)) {    
    $do_not_delete[] = $row['filename']; }

foreach(glob("*") as $filename) {
    if (!in_array($filename, $do_not_delete)) {
        //delete them
    }
}

I'm not too savvy with PHP, but I don't believe they are specifying a folder path on the server here, are they? I'd like to be able to look inside a specific folder and check whether any images in that folder are within any database tables. If not, delete that image.

  • 写回答

2条回答 默认 最新

  • duanshaiduhao2471 2017-05-27 20:58
    关注

    Before calling glob("*") just add a line chdir(""). Then you can search whichever directory you want to look at. I just went one level higher in the call below. You may specify whichever directory you want. Before doing a delete, just add an echo statement to $filename to verify if the correct files are being deleted.

    chdir("../");
    foreach(glob("*") as $filename) {
    if (!in_array($filename, $do_not_delete)) {
        //delete them
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!