我正在构建一个允许用户上传照片的 PHP 应用程序。为了便于管理,一个文件夹最多应有 5000 张照片。每张上传的照片都会在数据库中分配一个ID,照片会被重命名为ID。
如何查看某个文件夹是否有5000张照片?我应该检查照片表中最后一行的 ID 吗?
文件夹应递增命名。例如。 folder_1
、folder_2
等
我建议的步骤:
- 检索照片表的最后插入 ID
- (Last_insert_ID + 1) % 5000 = Some_number(应该保存的文件夹编号)
- 将照片保存到 folder_some_number。如果文件夹不存在,请创建一个新文件夹。
或者有更好的方法吗?
最佳答案
我猜数据库读取会比这样的文件系统读取更快。
你最好运行一个 sql 查询并获取每个文件夹的计数,分组。
// Example Query
SELECT COUNT(file), folderId As Count FROM photos WHERE folderId IN ('1', '2') GROUP BY folder
// It would be beneficial to have a flag on the folders that would enable or disable them
// that way you're not iterating through folders that we already know are > 5k
// You would run this and have seperate query that would pull in these folder names
// and passing them to the above query.
SELECT foldername, folderId FROM folders WHERE countFlag = 0;
//Example Conditional.
if($Count > 5000):
// Over 5k Do Something
//Since were over 5k, set this folders flag to 1
// that way we arent iterating through it again
$countFlag = 1;
else:
// Under 5k Do Something else
endif;
注意:如果您需要实际的代码示例,我可以快速编写一些东西。上面的示例省略了实际的工作代码,仅用于理论目的。 您需要遍历返回的行,因为它们按文件夹分组。
关于PHP整理上传的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10176346/