重命名文件(如果已存在) - php 上传系统


Rename a file if already exists - php upload system

我这个PHP代码:

<?php
// Check for errors
if($_FILES['file_upload']['error'] > 0){
    die('An error ocurred when uploading.');
}
if(!getimagesize($_FILES['file_upload']['tmp_name'])){
    die('Please ensure you are uploading an image.');
}
// Check filesize
if($_FILES['file_upload']['size'] > 500000){
    die('File uploaded exceeds maximum upload size.');
}
// Check if the file exists
if(file_exists('upload/' . $_FILES['file_upload']['name'])){
    die('File with that name already exists.');
}
// Upload file
if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], 'upload/' . $_FILES['file_upload']['name'])){
    die('Error uploading file - check destination is writeable.');
}
die('File uploaded successfully.');
?>

我需要像对现有文件的"Windows"处理一样 - 我的意思是如果文件存在,我希望它被更改为文件名后面的数字 1。

例如:myfile.jpg 已经存在,所以如果你再次上传它,它将是 myfile1.jpg,如果 myfile1.jpg 存在,它将是 myfile11.jpg依此类推......

我该怎么做? 我尝试了一些循环,但不幸的是没有成功。

你可以做这样的事情:

$name = pathinfo($_FILES['file_upload']['name'], PATHINFO_FILENAME);
$extension = pathinfo($_FILES['file_upload']['name'], PATHINFO_EXTENSION);
// add a suffix of '1' to the file name until it no longer conflicts
while(file_exists($name . '.' . $extension)) {
    $name .= '1';
}
$basename = $name . '.' . $extension;

为了避免名称很长,附加一个数字可能会更整洁,例如 file1.jpgfile2.jpg等:

$name = pathinfo($_FILES['file_upload']['name'], PATHINFO_FILENAME);
$extension = pathinfo($_FILES['file_upload']['name'], PATHINFO_EXTENSION);
$increment = ''; //start with no suffix
while(file_exists($name . $increment . '.' . $extension)) {
    $increment++;
}
$basename = $name . $increment . '.' . $extension;

对于用户6930268;我认为你的代码应该是:

$name = pathinfo($_FILES['file_upload']['name'], PATHINFO_FILENAME);
$extension = pathinfo($_FILES['file_upload']['name'], PATHINFO_EXTENSION);
$dirname = pathinfo($_FILES['file_upload']['name'], PATHINFO_DIRNAME);
$dirname = $dirname. "/";
$increment = ''; //start with no suffix
while(file_exists($dirname . $name . $increment . '.' . $extension)) {
    $increment++;
}
$basename = $name . $increment . '.' . $extension;
$resultFilePath = $dirname . $name . $increment . '.' . $extension);
  1. 您上传了一个名为 demo.png 的文件。
  2. 您尝试上传相同的文件demo.png,但它已重命名为 demo2.png
  3. 当您尝试第三次上传demo.png时,它会再次重命名为 demo1.png 并替换您在 (2) 中上传的文件。

所以你找不到demo3.png

这是我

正在使用的函数。它将生成文件 (1).txt 、文件 (2).txt 、文件 ...

function getFilePathUnique($path) {
    while ($this->location->fileExists($path)) {
        $info = pathInfo($path);
        //extract the current number of file
        preg_match("/'([0-9]+')$/",$info["filename"], $number);
        $number = str_replace(["(" , ")"] , ["" , ""] , $number[0]);
        //remove the old number
        $info["filename"] = trim(preg_replace( "/'([0-9]+')$/" , "" , $info["filename"] ));
        //append new number
        $info["filename"] .= " (" . (++$number) . ")";
        //build path
        $path = ($info["dirname"] != "." ? $info["dirname"]:  "" ).
            $info["filename"] . "." . $info["extension"];
    }           
    return $path;
    
}