我在文件系统中存储了大量文件,其中一些是 mp4、jpg 和 pdf
我想删除所有文件,除了一些从 mysql 查询中提取的文件。例如 RandomMovie_04.mp4、AwesomeImage.jpg
这是我的文件结构
files/
messages/
images/
45/ --> this is the ID in the DB
46/
articles/
images/
124/
125/
videos/
124/
207/
我不想删除的文件是从数据库中提取的
我正在使用 PHP (CakePHP) 和 MySQL
试过这个,它对一个文件有效,但我找不到用文件列表来做到这一点的方法
find * -maxdepth 5 -not -name "RandomMovie_04.mp4" -type f -exec ls "{}" \; > list.txt
这对两个人都有效
find * -maxdepth 5 -not -name "RandomMovie_04.mp4" -not -name "AwesomeImage.jpg" -type f -exec ls "{}" \; > list.txt
我必须对所有排除的文件执行此操作(在 php 脚本中)?喜欢 -not -name fileA -not -name fileB -not -name to_the_infinite...
谢谢
对不起,如果我的英语很烂 :(
最佳答案
如果您可以从 PHP 脚本中获取一组名称,则可以像这样为 find
创建字符串:
<?php
$files = array("RandomMovie_04.mp4", "AwesomeImage.jpg", "GreatDoc.pdf");
echo implode(' -or ', array_map(function($x) { return "-name '$x'"; }, $files));
哪些输出:
-name 'RandomMovie_04.mp4' -or -name 'AwesomeImage.jpg' -or -name 'GreatDoc.pdf'
您的 find
命令可以是:
find /path/to/dir -not \( $(php files.php) \) -delete
find
将删除所有未找到名称的文件。
请注意,您应该从单独的目录运行它,否则 PHP 文件将被删除。
关于php - 如何从数据库中删除除大列表之外的所有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25236803/