自动递增文件名?

Auto increment file name?

我想制作我的上传文件网站,所以当人们将文件上传到服务器时,我希望服务器递增文件名,例如

那么,我如何增加文件名?这是我的代码

        <?php
 $limitsize = 1000000;
 $target_pics = "uploads/user/pics/" . basename($_FILES["fileToUpload"]["name"]);
 $target_video = "uploads/user/video/" . basename($_FILES["fileToUpload"]["name"]);
 $target_other = "uploads/user/other/" . basename($_FILES["fileToUpload"]["name"]);
 $FileType = strtolower(pathinfo($target_video, PATHINFO_EXTENSION));
 $uploadOk = 1;


 // Check file exists

 if (file_exists($target_pics) || file_exists($target_video) || file_exists($target_other)) {
   echo "Sorry, file already exists. <br>";
   $uploadOk = 0
 }


 // Check file size

 if ($_FILES["fileToUpload"]["size"] > $limitsize) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
 }


 if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
 } else {
    if ($FileType == "jpg" || $FileType == "png" || $FileType == "jpeg" || $FileType == "gif" ) {
       move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_pics);
       echo "Upload Success.";
    } else {
       if ($FileType == "mp4" || $FileType == "avi") {
       move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_video);
       echo "Upload Success";
       } else {
           move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_other);
           echo "Upload Success";
         }
       }
     }
?>

我不知道如何在 php 中完成此任务,但我可以使用 java 为您提供解决方案 在 table 映射 xml 文件中的休眠映射中,在这个 class 的帮助下,有一个标签调用它的 s a class 你可以做一件事 id 放在生成器标签中时您上传文件,它将以其实际名称存储,但自动生成 ID 升序排列,您可以显示为 id.extension(如 2.png),用标签括起来,它是一个链接文本,当您想要下载时您只需单击相应的 id.extension linkText 并从 linkText 中删除 .extension 用作 id 并在 id 的帮助下从数据库中获取数据的文件.. 我希望它对你有用。

根据我的理解,您需要将上传的文件存储在服务器上,同目录下的文件名称编号。如果是这样,您可以使用以下代码来更新您发布的代码:

<?php
  $limitsize = 1000000;
  $target_pics = "uploads/user/pics/" . basename($_FILES["fileToUpload"]["name"]);
  $target_video = "uploads/user/video/" . basename($_FILES["fileToUpload"]["name"]);
  $target_other = "uploads/user/other/" . basename($_FILES["fileToUpload"]["name"]);
  $FileType = strtolower(pathinfo($target_video, PATHINFO_EXTENSION));
  $uploadOk = 1;


  // Check file exists
  if (file_exists($target_pics) || file_exists($target_video) || file_exists($target_other)) {
   echo "Sorry, file already exists. <br>";
   $uploadOk = 0
 }


 // Check file size
 if ($_FILES["fileToUpload"]["size"] > $limitsize) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
 }


 if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
 } else {
    if ($FileType == "jpg" || $FileType == "png" || $FileType == "jpeg" || $FileType == "gif" ) {
       $num = count(scandir($target_pics)); // scandir: gets an array of the files in the directory
       move_uploaded_file($_FILES["fileToUpload"]["tmp_name"] . ($num + 1), $target_pics);
       echo "Upload Success.";
    } else {
       if ($FileType == "mp4" || $FileType == "avi") {
       $num = count(scandir($target_video));
       move_uploaded_file($_FILES["fileToUpload"]["tmp_name"] . ($num + 1), $target_video);
       echo "Upload Success";
       } else {
       $num = count(scandir($target_other));
           move_uploaded_file($_FILES["fileToUpload"]["tmp_name"] . ($num + 1), $target_other);
           echo "Upload Success";
         }
       }
     }
?>

在上传的文件夹中找到最新创建的文件,并使用该文件+1 名称重命名新文件。就这些了...