找出目录中哪些文件不是& amp; quot;在数据库中列出 [英] Find out what files are in a directory that are "not" listed in the database

查看:45
本文介绍了找出目录中哪些文件不是& amp; quot;在数据库中列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型画廊网站,但是目录中的图像数量与数据库中的图像数量相差约150,所以我想知道是否有一种方法可以找出/列出目录中哪些文件不是""(反之亦然).

I have a small gallery website but the amount of images in directory differs from that accounted for in database by around 150 so I was wondering if there is a way to find out/list what files are in a directory that are "not" in the database (or vice versa).

基本数据库结构:

  • 图片
    • id
    • 图片

    图像名称作为"imagename.jpg"存储在数据库中

    image names are stored in db as "imagename.jpg"

    ,图像本身存储在图像目录中

    and the images themselves are stored in images directory

    图片/

    推荐答案

    将服务器中的文件名放在一个数组中,将数据库中的文件放在另一个数组中.使用 array_diff()来获得结果.

    Put file names from server in one array and files from database in another. Use array_diff() to get result.

    示例(PHP):

    $files_db = array("car.jpg", "bike.jpg", "plane.jpg", "ship.jpg", "tank.jpg"); 
    $files_server = array("car.jpg", "bike.jpg", "ship.jpg", "rocket.jpg"); 
    

    使用

    print_r(array_diff($files_db, $files_server));
    

    输出

    Array
    (
        [2] => plane.jpg
        [4] => tank.jpg
    )
    

    或者(反之亦然)

    print_r(array_diff($files_server, $files_db));
    

    输出

    Array
    (
        [3] => rocket.jpg
    )
    

    这篇关于找出目录中哪些文件不是& amp; quot;在数据库中列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆