如何在每次执行PHP程序时为输出文件(.xml文件)生成新文件名

如何在每次执行PHP程序时为输出文件(.xml文件)生成新文件名,php,xml,file,filenames,sitemap,Php,Xml,File,Filenames,Sitemap,我正在运行一个PHP程序,它生成一个.XML格式的站点地图输出。对于我来说,每次运行PHP程序时,站点地图(格式为.xml)都应该有新的(不重复的)文件名,第一次显示为hello1.xml 第二次运行同一个php程序时,应该得到hello2.xml,第三次是hello3.xml My .php program generate the output in same server in .xml format.The name of the output file should be change

我正在运行一个PHP程序,它生成一个.XML格式的站点地图输出。对于我来说,每次运行PHP程序时,站点地图(格式为.xml)都应该有新的(不重复的)文件名,第一次显示为hello1.xml 第二次运行同一个php程序时,应该得到hello2.xml,第三次是hello3.xml

My .php program generate the output in same server in .xml format.The name of the output file should be changed automatically each time when run Php program.
请帮忙。

试试下面的代码

function getNewFileName($destFile)
{           
        $fileInfo = pathinfo($destFile);         
        if (file_exists($destFile)) {
            $index = 1;
            $baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
            while((file_exists($fileInfo['dirname'] . DIRECTORY_SEPARATOR . $baseName)) ) {
                $baseName = $fileInfo['filename']. '_' . $index . '.' . $fileInfo['extension'];
                $index ++;
            }            
            $destFileName = $baseName;                                  
        } else {
            return $fileInfo['basename'];
        }

        return $destFileName;
}
试试下面的代码

function getNewFileName($destFile)
{           
        $fileInfo = pathinfo($destFile);         
        if (file_exists($destFile)) {
            $index = 1;
            $baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
            while((file_exists($fileInfo['dirname'] . DIRECTORY_SEPARATOR . $baseName)) ) {
                $baseName = $fileInfo['filename']. '_' . $index . '.' . $fileInfo['extension'];
                $index ++;
            }            
            $destFileName = $baseName;                                  
        } else {
            return $fileInfo['basename'];
        }

        return $destFileName;
}

您需要创建递归函数来检查文件是否存在于您的路径中,如果已经存在则重命名。您可以在文件名中添加日期时间戳。你好(20160906123051)。是的@Sasikumarhx这也很好,但如果他想要自动索引1,2,3,他需要为此创建函数谢谢Minesh patel和Sasikumarhx我同意解决方案,谢谢。您需要创建递归函数来检查路径中是否存在文件,如果已经存在,请重命名。您可以在文件名中添加日期时间戳。你好(20160906123051)。是的@Sasikumarhx这也很好,但如果他想要自动索引1,2,3,他需要为此创建函数谢谢Minesh patel和Sasikumarhx我同意解决方案,谢谢。非常感谢,我同意解决方案好的。上面的解决方案根据该目录中可用的文件创建文件名,如实际文件名_1、实际文件名_2。非常感谢,我使用了该解决方案,非常好。上述解决方案基于该目录中可用的文件创建文件名,如实际文件名_1、实际文件名_2。