如何在php中增加文件名以防止重复

如何在php中增加文件名以防止重复,php,Php,在许多情况下,我们需要在创建文件名时使服务器上的文件名不同,以防止重复。最常见的答案似乎是,在末尾附加时间戳或将文件名存储在数据库中,然后使用这些存储的值来计算新名称。这些都很好,但是,附加一个长时间戳并不总是非常方便用户,并且在数据库中存储也不总是一个选项。那么,我们如何在通过php创建标准文件名时自动增加文件名?我为此编写了一个简单的函数: function incrementFileName($file_path,$filename){ if(count(glob($file_path.

在许多情况下,我们需要在创建文件名时使服务器上的文件名不同,以防止重复。最常见的答案似乎是,在末尾附加时间戳或将文件名存储在数据库中,然后使用这些存储的值来计算新名称。这些都很好,但是,附加一个长时间戳并不总是非常方便用户,并且在数据库中存储也不总是一个选项。那么,我们如何在通过php创建标准文件名时自动增加文件名?

我为此编写了一个简单的函数:

function incrementFileName($file_path,$filename){
 if(count(glob($file_path.$filename))>0)
 {
     $file_ext = end(explode(".", $filename));
     $file_name = str_replace(('.'.$file_ext),"",$filename);
     $newfilename = $file_name.'_'.count(glob($file_path."$file_name*.$file_ext")).'.'.$file_ext;
     return $newfilename;
  }
  else
  {
     return $filename;
  }
}
用法:

$newName = incrementFileName( "uploads/", $_FILES["my_file"]["name"] );
move_uploaded_file($_FILES["my_file"]["tmp_name"],"uploads/".$newName);

所有这些答案似乎都有些过分:

$k = 0;
while(!$result){
    if(!file_exists("file[$k].ext"))
        $result = "file[$k].ext";
    $k++;
}
makefile($result);

下面是一个简短的代码片段,演示如何开始解决此问题

// handle filename collision:
if(file_exists($newFile)) {
    // store extension and file name
    $extension = pathinfo($newFile,PATHINFO_EXTENSION);
    $filename = pathinfo($newFile, PATHINFO_FILENAME);

    // Start at dup 1, and keep iterating until we find open dup number
    $duplicateCounter = 1;
    // build a possible file name and see if it is available
    while(file_exists($iterativeFileName =
                        $newPath ."/". $filename ."_". $duplicateCounter .".". $extension)) {
        $duplicateCounter++;
    }

    $newFile = $iterativeFileName;
}

// If we get here, either we've avoided the if statement altogether, and no new name is necessary..
// Or we have landed on a new file name that is available for our use.
// In either case, it is now safe to create a file with the name $newFile

欢迎来到堆栈溢出!感谢您提供的代码片段,它可能会提供一些有限的、即时的帮助。一个恰当的解释将通过描述为什么这是一个很好的问题解决方案来极大地改进its,并将使它对未来有其他类似问题的读者更有用。请编辑您的答案以添加一些解释,包括您所做的假设。这应该是选定的答案。它对我的用例进行了一点修改,效果非常好。回答得很好,节省了我很多时间!计数不一定代表最大的数字。相反,它将指向现有文件。函数应该找到最大的数字并递增。这是了解概念代码如何工作的一个良好开端,但它假定所有文件名都用相同的文件[#].ext filename括起来。Jade Steffen的代码/答案适用于任何文件名。
// handle filename collision:
if(file_exists($newFile)) {
    // store extension and file name
    $extension = pathinfo($newFile,PATHINFO_EXTENSION);
    $filename = pathinfo($newFile, PATHINFO_FILENAME);

    // Start at dup 1, and keep iterating until we find open dup number
    $duplicateCounter = 1;
    // build a possible file name and see if it is available
    while(file_exists($iterativeFileName =
                        $newPath ."/". $filename ."_". $duplicateCounter .".". $extension)) {
        $duplicateCounter++;
    }

    $newFile = $iterativeFileName;
}

// If we get here, either we've avoided the if statement altogether, and no new name is necessary..
// Or we have landed on a new file name that is available for our use.
// In either case, it is now safe to create a file with the name $newFile