2010-01-16 207 views
2

的文件名我想在文件夾中創建一個新文件,其中包含名稱以數字順序排列的現有文件,例如。 1,2,3,4 ...基於文件夾中已有的名稱創建名稱爲

我想檢查什麼是最後的nr,然後用一個nr創建一個文件。

我知道我應該使用file_exists,但我不知道如何使用它,在for循環也許?但是如何?

將是很好,如果有人可以給我一個提示

回答

5

我覺得這是你最好的選擇(revisions以前版本):

$files = glob('/path/to/dir/*');  // get all files in folder 
natsort($files);       // sort 
$lastFile = pathinfo(array_pop($files)); // split $lastFile into parts 
$newFile = $lastFile['filename'] +1; // increase filename by 1 

if(file_exists("/path/to/dir/$newFile")) { // do not write file if it exists 
    die("$newFile aready exists"); 
} 
file_put_contents("/path/to/dir/$newFile", 'stuff'); // write new file  

只要文件夾中的文件名從數字開始,這應該總是寫入最高編號的文件名加1,例如

1,5,10     => writes file 11 
1.txt, 5.gif, 10.jpg => writes file 11 
1, 5.txt, 10_apple.txt => writes file 11 

如果有一個文件開頭的號碼,上面的辦法是行不通的,因爲數字字符之前排序,因此沒有什麼會爲例如寫

1,5,10,foo => foo+1 equals 1, already exists, nothing written 

您可以通過更改爲水珠圖案來/path/[0-9]*,那麼這將只匹配開始與一些文件解決這個問題。那應該是相當穩固的。

注意natsort在不同的操作系統上表現不同。上述工作在我的Windows機器上正常工作,但您需要檢查生成的排序順序以使其適用於您的特定機器。

有關如何使用glob()natsort()pathinfo()的詳細信息,請參見手冊;

+0

所以1.JPG和2.gif不會事宜或是U假設他們得到了相同的擴展? – ajsie 2010-01-16 11:38:02

+1

glob中的星號與文件夾中的所有文件匹配,無論擴展名如何。 – Gordon 2010-01-16 11:41:20

+0

如果你刪除了一個文件(已經過時或者其他文件),那麼這將失敗,因爲文件數量不等於最新的'id-number' – Veger 2010-01-16 11:52:33

2

也許這樣?

$name = 'filename'; 
$ext = '.txt'; 
$i = 1; 
$tmpname = $name . $ext; 
while(file_exists($tmpname)) { 
    $i++; 
    $tmpname = $name . $i . $ext; 
} 

// $tmpname will be a unique filename by now 
+0

乾淨漂亮,但應該指出的是,它會寫入不在目錄中的第一個文件,例如當有10個文件,我刪除1.txt時,上面的代碼會在下一次運行時創建1.txt,而不是11.txt – Gordon 2010-01-16 14:16:01

1

單程。想象一下文件名的1.txt,2.txt等

$dir = "/path"; 
chdir($dir); 
$files = glob("[0-9]*.txt"); 
print "Files aftering globbing: "; 
print_r($files); 
sort($files,SORT_NUMERIC); 
print "After sorting using numeric sort: "; 
print_r($files); 
# get latest file 
$newest=end($files); 
$s=explode(".",$newest); 
$s[0]=$s[0]+1; 
$newname=$s[0].".txt"; 
touch($newname); 

輸出

$ ls *txt 
10.txt 11.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 

$ php test.php 
Files aftering globbing: Array 
(
    [0] => 1.txt 
    [1] => 10.txt 
    [2] => 11.txt 
    [3] => 2.txt 
    [4] => 3.txt 
    [5] => 4.txt 
    [6] => 5.txt 
    [7] => 6.txt 
    [8] => 7.txt 
    [9] => 8.txt 
    [10] => 9.txt 
) 
After sorting using numeric sort: Array 
(
    [0] => 1.txt 
    [1] => 2.txt 
    [2] => 3.txt 
    [3] => 4.txt 
    [4] => 5.txt 
    [5] => 6.txt 
    [6] => 7.txt 
    [7] => 8.txt 
    [8] => 9.txt 
    [9] => 10.txt 
    [10] => 11.txt 
) 

$ ls *.txt 
10.txt 11.txt **12.txt** 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 
+1

我最近看到的最糟糕的一段代碼,** 1)** glob ()'需要知道'$ dir',** 2)**你需要使用{0,1,2,3,4,5,6,7,8,9}和GLOB_BRACE標誌,例如,'t匹配10.txt'和** 3)**'glob()'已經排序的東西,爲什麼要重新排序呢? – 2010-01-16 11:53:37

+0

你應該去試一試,看看。創建文件1.txt到20.txt。然後執行正常的glob,然後使用數字+通配符()排序()。你會看到不同之處。 – ghostdog74 2010-01-16 12:46:40

+0

嘗試使用與當前目錄不同的路徑執行此操作。在我的Windows機器上,排序看起來不同。它會下降,而不是按正確的順序,可能是因爲我在目錄中也有文件123_foo.txt。 – Gordon 2010-01-16 13:27:11

相關問題