自动增加文件名?

时间:2018-11-04 08:10:51

标签: php upload increment

我想建立自己的上传文件网站,以便当人们将文件上传到服务器时,我希望服务器增加文件名,例如

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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