开发者

create file with name based on what already exist in folder

开发者 https://www.devze.com 2022-12-16 09:24 出处:网络
i want to create a new file in a folder with existing files with names in a numerical order eg. 1, 2, 3, 4...

i want to create a new file in a folder with existing files with names in a numerical order eg. 1, 2, 3, 4...

i want to check what is the last nr and then create a file with one nr over that one.

i know i should use file_exists but i don't know exactly how to use it, in a for loop maybe? but how?

would be nice if s开发者_高级运维omeone could give me a hint


I think this is your best bet (see revisions for previous versions):

$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    

As long as your filenames in the folder start with numbers, this should always write the highest numbered filename incremented by one, e.g.

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

If there is a file not starting with a number, the above approach won't work, because numbers are sorted before characters and thus nothing would be written for e.g.

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

You can get around this by changing the pattern for glob to /path/[0-9]*, which would then only match files starting with a number. That should be pretty solid then.

Note natsort behaves different on different OS. The above works fine on my Windows machine, but you will want to check the resulting sort order to get it working for your specific machine.

See the manual for further info on how to use glob(), natsort() and pathinfo();


Maybe like this?

$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


one way. imagine filenames 1.txt, 2.txt etc

$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);

output

$ 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号