1

在php中,我们可以检查文件是否存在使用

if(file_exists("destination/"))
{
    condition
}

但我想做的是...

例如,我的目的地已经有了这个文件

hello_this_is_filename(2).doc

我怎么知道该目录中是否有名称包含字符的文件

hello_this_is_filename

我想以这种方式搜索,因为...如果该目录上存在,我将做的是...将文件重命名为

hello_this_is_filename(3).doc

我还需要计算我的搜索是否存在,所以我知道我要输入什么数字

(3), (4), (5) and so on

有什么帮助吗?

4

2 回答 2

5

使用glob

if (count(glob("destination/hello_this_is_filename*.doc"))) {
  //...
}
于 2012-09-14T02:53:34.003 回答
0

利用 Marc B 的建议和 xdazz,我将执行以下操作:

<?php
$files = glob("destination/hello_this_is_filename*");
if (count($files)) {
   sort($files);

   // last one contains the name we need to get the number of
   preg_match("([\d+])", end($files), $matches);

   $value = 0;
   if (count($matches)) {
      // increment by one
      $value = $matches[0];
   }

   $newfilename = "destination/hello_this_is_filename (" . ++$value . ").doc";
?>

抱歉,这是未经测试的,但认为它为其他人提供了正则表达式工作来实际执行递增...

于 2012-09-14T03:42:25.250 回答