如果已存在具有该名称的图像,则将数字附加到文件名

问题描述:

I wrote some php that checks if a file already exists when a user uploads a new file, and if so appends a string to the filename. What I want is that a number is added to the end of the filename and when another file with the same name is uploaded it tops the number by 1.

So for example: I already have an image uploaded with filename image.png and I upload another one with the same filename. That file should be renamed to image0.png. When I try to upload another image with filename image.png it should be renamed to image1.png and so on.

I'm not sure how to accomplish this within my code. Can anyone help me out? This is the code snippet that checks for duplicates and appends something to the filename.

if(file_exists("/customers/d/8/e/frankkluytmans.nl/httpd.www/testsite/cms/upload/".$_FILES["image"]["name"]))
{
    $filename = explode(".",$_FILES['image']['name']);
    $imageName = $filename[0]."hoi.".$filename[1];
}
else
{
    $imageName = $_FILES['image']['name'];
}

$image = mysql_real_escape_string(htmlspecialchars("/upload/".$imageName));

if (move_uploaded_file($_FILES["image"]["tmp_name"],     "./upload/".$imageName)) {�

mysql_query("INSERT frankkluytmans SET pagid='$pagid', title='$titlename', content='$contentname', image='$image', youtube='$youtube'")
or die(mysql_error()); 

header("Location: index.php"); 

}

我写了一些php,用来检查用户上传新文件时文件是否已经存在,如果有的话附加一个 字符串到文件名。 我想要的是在文件名末尾添加一个数字,当上传另一个同名文件时,它将数字加1。 p>

例如:我已经有了 使用filename image.png上传的图像,我上传另一个文件名相同的图像。 该文件应重命名为image0.png。 当我尝试使用filename image.png上传另一个图像时,它应该重命名为image1.png,依此类推。 p>

我不知道如何在我的代码中完成此操作。 谁能帮我吗? 这是检查重复项并在文件名中附加内容的代码片段。 p>

  if(file_exists(“/ customers / d / 8 / e / frankkluytmans.nl / httpd。  www / testsite / cms / upload /".$_ FILES [“image”] [“name”]))
 {
 $ filename = explode(“。”,$ _ FILES ['image'] ['name']  ); 
 $ imageName = $ filename [0]。“hoi。”。$ filename [1]; 
} 
else 
 {
 $ imageName = $ _FILES ['image'] ['name'];  
 
 
 $ image = mysql_real_escape_string(htmlspecialchars(“/ upload /".$ imageName)); 
 
if(move_uploaded_file($ _ FILES [”image“] [”tmp_name“],”。/ upload /  “。$ imageName)){�
 
mysql_query(”INSERT frankkluytmans SET pagid ='$ pagid',title ='$ titlename',content ='$ contentname',image ='$ image',youtube ='$ youtube  '“)\也不死(mysql_error());  
 
header(“Location:index.php”);  
 
} 
  code>  pre> 
  div>

You could use a recursive function to keep checking the number:

function checkFile($path, $file, $ext, $number)
{
    if(file_exists($path.$file.$number.$ext))
    {
        if($number == "")
            $number = 0;

        $number ++;
        return checkFile($path, $file, $ext, $number);
    }

    return $path.$file.$number.$ext;
}

//run mime type things to get extension. for now lets pretend it is .jpg
// and assume you stripped the extension from the uploaded file...
// also, pass blank to last param to start the file check without a number.

$extension = ".jpg";
$filename_minus_extension = "some-image";

$filename = checkFile('path-to-directory/', $filename_minus_extension, $extension, "");

This is in no way tested, but the general idea is to use a recursive function. If you have a specific question, feel free to ask and I may update.

You need some kind of loop because you don't know how often you have to check. Then simply change the name in the wanted pattern until there is no file existing with the chosen name

$info = pathinfo($filename); // get details
$name = $info['filename'];
$extension = $info['extension'];
$counter = 0;

while ( file_exists( $name.$counter.'.'.$extension ) )
{
    $counter++;
}

$new_name = $name.$counter.'.'.$extension;

The code is not tested but should work. More importantly I hope you understand what it does ;)