protected function renameDuplicateFile($file_name, $loop_count = 1)
    {
        if (!file_exists($this->move_uploaded_to.DIRECTORY_SEPARATOR.$file_name)) {
            return $file_name;
        } else {
            $file_name_explode = explode('.', $file_name);
            $file_extension = (isset($file_name_explode[count($file_name_explode)-1]) ? $file_name_explode[count($file_name_explode)-1] : null);
            unset($file_name_explode[count($file_name_explode)-1]);
            $file_name_only = implode('.', $file_name_explode);
            unset($file_name_explode);

            $i = 1;
            $found = true;
            do {
                $new_file_name = $file_name_only.'_'.$i.'.'.$file_extension;
                if (file_exists($this->move_uploaded_to.DIRECTORY_SEPARATOR.$new_file_name)) {
                    $found = true;
                    if ($i > 1000) {
                        // too many loop
                        $file_name = uniqid().'-'.str_replace('.', '', microtime(true));
                        $found = false;
                    }
                } else {
                    $file_name = $new_file_name;
                    $found = false;
                }
                $i++;
            } while ($found === true);

            unset($file_extension, $file_name_only, $new_file_name);
            return $file_name;
        }
    }
protected function renameDuplicateFile($file_name, $loop_count = 1)
    {
        if (!file_exists($this->move_uploaded_to.DIRECTORY_SEPARATOR.$file_name)) {
            return $file_name;
        } else {
            $file_name_explode = explode('.', $file_name);
            $file_extension = (isset($file_name_explode[count($file_name_explode)-1]) ? $file_name_explode[count($file_name_explode)-1] : null);
            unset($file_name_explode[count($file_name_explode)-1]);
            $file_name_only = implode('.', $file_name_explode);
            unset($file_name_explode);

            $i = 1;
            $found = true;
            do {
                $new_file_name = $file_name_only.'_'.$i.'.'.$file_extension;
                if (file_exists($this->move_uploaded_to.DIRECTORY_SEPARATOR.$new_file_name)) {
                    $found = true;
                    if ($i > 1000) {
                        // too many loop
                        $file_name = uniqid().'-'.str_replace('.', '', microtime(true));
                        $found = false;
                    }
                } else {
                    $file_name = $new_file_name;
                    $found = false;
                }
                $i++;
            } while ($found === true);

            unset($file_extension, $file_name_only, $new_file_name);
            return $file_name;
        }
    }
protected function renameDuplicateFile ( $ file_name , $ loop_count = 1 ) { if ( ! file_exists ( $ this -> move_uploaded_to . DIRECTORY_SEPARATOR . $ file_name ) ) { return $ file_name ; } else { $ file_name_explode = explode ( '.' , $ file_name ) ; $ file_extension = ( isset ( $ file_name_explode [ count ( $ file_name_explode ) - 1 ] ) ? $ file_name_explode [ count ( $ file_name_explode ) - 1 ] : null ) ; unset ( $ file_name_explode [ count ( $ file_name_explode ) - 1 ] ) ; $ file_name_only = implode ( '.' , $ file_name_explode ) ; unset ( $ file_name_explode ) ; $ i = 1 ; $ found = true ; do { $ new_file_name = $ file_name_only . '_' . $ i . '.' . $ file_extension ; if ( file_exists ( $ this -> move_uploaded_to . DIRECTORY_SEPARATOR . $ new_file_name ) ) { $ found = true ; if ( $ i > 1000 ) { // too many loop $ file_name = uniqid ( ) . '-' . str_replace ( '.' , '' , microtime ( true ) ) ; $ found = false ; } } else { $ file_name = $ new_file_name ; $ found = false ; } $ i ++ ; } while ( $ found === true ) ; unset ( $ file_extension , $ file_name_only , $ new_file_name ) ; return $ file_name ; } }
Rename the file where it is duplicate with existing file.

@param string $file_name File name to check
@return string Return renamed file that will not duplicate the existing file.
Rename the file where it is duplicate with existing file .