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 に答える