【问题标题】:Auto increment file name?自动增加文件名?
【发布时间】:2018-11-04 08:10:50
【问题描述】:

我想制作我的上传文件网站,所以当人们将文件上传到服务器时,我希望服务器增加文件的名称喜欢

  • 1.png
  • 2.png
  • 3.png

那么,我怎样才能增加文件名呢?这是我的代码

        <?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 upload increment


【解决方案1】:

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

【讨论】:

  • 由于 OP 在 PHP 中寻求解决方案,因此这可能不是很有用的答案。
【解决方案2】:

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

<?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";
         }
       }
     }
?>

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 2019-02-02
      • 2017-04-21
      • 1970-01-01
      • 2020-05-03
      • 2021-12-27
      相关资源
      最近更新 更多