2010-01-16 183 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

相关问题