简体   繁体   English

如果文件已经存在,则重命名文件 -php 文件上传

[英]Rename a file if already exists -php file upload

i want to rename a uploaded file if already exist.如果已经存在,我想重命名上传的文件。 i want to rename it into img1.jpg,img2.jpg if already exist.如果已经存在,我想将其重命名为 img1.jpg,img2.jpg。 i tried many example but nothing suits for this code below:我尝试了很多例子,但没有什么适合下面的代码:

 <?php $valid_formats = array( "jpg", "png", "gif", "zip", "bmp", "pdf", "docx", "PDF", "xlxc" ); $max_file_size = 3024; $path = "images/"; // Upload directory $count = 0; if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") { $fname = $_FILES['attach']['name']; foreach ($_FILES['attach']['name'] as $f => $name) { if ($_FILES['attach']['error'][$f] == 4) { continue; } if ($_FILES['attach']['error'][$f] == 0) { if ($_FILES['attach']['size'][$f] > $max_file_size) { $message[] = "$name is too large!."; continue; } elseif (!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) { $message[] = "$name is not a valid format"; continue; } else { if (move_uploaded_file($_FILES["attach"]["tmp_name"][$f], $path . $name)) $count++; } } } } ?>

need some update for file exisits in the above code .需要对上述代码中的文件进行一些更新。

Try this:尝试这个:

before moving the file to server put this check

$fname = $_FILES['attach']['name'];
if(file_exist($path))
{
    $file = explode($fname);    // explode file name and extension

    $rand = rand(1, 100);
    $fname = $file[0].'.'.$rand.$file[1];   // combine file name with some random value with extension
}

// Now fname will contain the original name or modifed name in it
// You can maintain a counter instead of random value that gives you 1,2,3 ...

You could try replacing你可以尝试更换

else{  
    if(move_uploaded_file( $_FILES["attach"]["tmp_name"][$f], $path.$name ))
    $count++; 

}

with

else {

    $targetfile = $path . $name;

    if( file_exists( $targetfile ) ){

        /* get filename & extension from target file */
        $filename=pathinfo( $targetfile, PATHINFO_FILENAME );
        $extension=pathinfo( $targetfile, PATHINFO_EXTENSION );

        /* if the last character of the file's name is a digit, increment by one */
        $lastchar = intval( substr( $filename, strlen( $filename )-1 ) );

        if( $lastchar && !is_nan( $lastchar ) ) {
            /* Get the actual integer value from the end of the filename */
            preg_match( '@\d+$@', $filename, $matches );
            if( !empty( $matches ) ) $lastchar=$matches[0];

            /* Remove original number and replace with new incremented value */
            $filename = substr( $filename, 0, strlen( $filename ) - strlen( $lastchar ) ) . ( $lastchar + 1 );

        } else {
            /* last character was not a digit, add '1' to filename */
            $filename.='1';
        }

        /* determine new file's path */
        $targetfile = $path . $filename . '.' . $extension;

        clearstatcache();
    }

    if( move_uploaded_file( $_FILES["attach"]["tmp_name"][$f], $targetfile ) ) $count++;
}

It should determine whether the file already exists in the designated target folder - if it does and has an integer as the last part to it's name it should increment that value by one and create a new filename.它应该确定文件是否已经存在于指定的目标文件夹中 - 如果它存在并且名称的最后一部分是一个整数,它应该将该值增加一并创建一个新的文件名。 If there is no integer it should simply add '1' - but if the file does not exist it will use the original name.如果没有整数,它应该简单地添加“1” - 但如果文件不存在,它将使用原始名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM