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

时间:2016-10-12 06:40:51

标签: php file-upload file-exists

我想重命名已上传的文件(如果已存在)。我想将它重命名为img1.jpg,img2.jpg如果已经存在的话。我尝试了很多例子,但下面的代码不适用:

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

需要对上述代码中的文件存在进行一些更新。

2 个答案:

答案 0 :(得分:0)

试试这个:

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 ...

答案 1 :(得分:0)

您可以尝试替换

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

}

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++;
}

它应该确定文件是否已经存在于指定的目标文件夹中 - 如果它存在并且有一个整数作为它的名称的最后一部分它应该将该值递增1并创建一个新文件名。如果没有整数,则只需添加&#39; 1&#39; - 但如果文件不存在,它将使用原始名称。