PHP整理上传的照片

标签 php image upload file-organization

我正在构建一个允许用户上传照片的 PHP 应用程序。为了便于管理,一个文件夹最多应有 5000 张照片。每张上传的照片都会在数据库中分配一个ID,照片会被重命名为ID。

如何查看某个文件夹是否有5000张照片?我应该检查照片表中最后一行的 ID 吗?

文件夹应递增命名。例如。 folder_1folder_2

我建议的步骤:

  1. 检索照片表的最后插入 ID
  2. (Last_insert_ID + 1) % 5000 = Some_number(应该保存的文件夹编号)
  3. 将照片保存到 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/

相关文章:

php - 使用 PHP 将图像 blob 从 mysql 数据行插入 mysql 数据库

php - 文件上传时更改文件权限——PHP?

html - 将大图像加载到 svg 组件中

Python:通过 FTP 上传大量文件

Python App Engine 上传图片内容类型

PHP exec不执行命令

php - 表格单元格/二维数组排序算法

php - 从其他表 + Doctrine 查询

php - 修改(创建 PHP 扩展)Makefile 以包含 C++ 类和代码

Android - 多次使用图像资源但仅在 1 个实例上更改 alpha