2016-09-06 58 views
1

我正在運行PHP程序,它生成輸出爲.XML格式的站點地圖映射。對於我每次運行PHP程序時,站點地圖(以.xml格式)都應該有新的(非重複的)文件名,第一次說hello1.xml第二次當我運行同樣的php程序時,它應該得到hello2.xml ,第三次hello3.xml ................如何在每次執行PHP程序時爲輸出文件(.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. 

請幫忙。

+0

你需要的,如果已經存在 –

+0

正如你可以添加日期時間戳文件名備用創造遞歸函數來檢查路徑文件中存在,並重新命名。例如。 hello_20160906123051。這將永遠是獨一無二的,也容易訂購。 – Sasikumar

+0

是@Sasikumarhx這也很好,但如果他想自動索引1,2,3他需要創建該功能 –

回答

0

試試下面的代碼

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

非常感謝,我解決了問題 <?php $ date = date_create(); echo date_timestamp_get($ date); ?> –

+0

好的。上面的解決方案創建文件名稱,如實際文件名稱_1,根據該目錄中可用文件的實際文件名稱__2。 –

相關問題