0

10個の.txtファイルがあるディレクトリがあります。次を使用してディレクトリを開きます。

$dir_handle = @opendir($path) or die("Unable to open $path"); 

テキストファイルのファイル名を読み取ってから、それらすべてのファイル名を|を使用してマスターリストに書き込むことができる必要があります。各ファイル名の後。また、masterlist.txtをそれ自体の中に記述したくありません(笑)。したがって、masterlist.txtは、.txtファイルのファイル名で書き込まれるものです。

4

1 に答える 1

1

でディレクトリをループし、readdir()各エントリがマスターリストまたは.またはではないことを確認し..ます。を使用してファイルに書き込みますfile_put_contents()

// If master.txt already exists, delete it.
if (file_exists('master.txt')) {
  unlink('master.txt');
}

$dir_handle = @opendir($path) or die("Unable to open $path"); 
while ($f = readdir($dir_handle)) {
  if ($f != '.' && $f != '..' && $f != 'master.txt') {
    file_put_contents('master.txt', $f . "|", FILE_APPEND);
  }
}

.txtファイルのみが必要な場合は、次のようなものを使用します。

if (preg_match('/^(.+)\.txt$/', $f)) {
  // it's a .txt file.
}
于 2011-07-27T18:26:30.173 に答える