PHP组织上传的照片

Web*_*ice 5 php upload file-organization image

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

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

文件夹应逐步命名.例如.folder_1,folder_2等等.

我建议的步骤:

  1. 检索照片表的最后一个插入ID
  2. (Last_insert_ID + 1)%5000 = Some_number(应保存的文件夹编号)
  3. 将照片保存到folder_some_number.如果文件夹不存在,请创建新文件夹.

或者有更好的方法吗?

gor*_*ive 5

我猜数据库读取会比这样的文件系统读取更快.

您最好运行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;
Run Code Online (Sandbox Code Playgroud)

注意:如果您需要实际的代码示例,我可以快速鞭打一些东西.以上示例省略了实际工作代码,仅用于理论目的.您将需要迭代返回的行,因为它们按文件夹分组.


Opt*_*ime 2

$last_id; // for example 9591
$current_folder = floor($last_id/5000);
$picture_num_in_folder = $last_id-($current_folder*5000);
if ($picture_num_in_folder == 5000)
    // new dir and such (new folderid = $current_folder+1 and $picture_num_in_folder = 1)
else
    // just place the picture with unique ID $last_id+1 called image_$picture_num_in_folder+1 in folder $current_folder
Run Code Online (Sandbox Code Playgroud)