Php 如何循环所有文件夹并将其打开,读取放在每行末尾的文件,\n并使其成为单行,然后将其另存为新文件
我有一个目录,其中只包含很多文件夹,每个文件夹都有一个包含URL的文本文件。我尝试创建一个php代码,在每个文件夹上打开该文本文件,并对其进行编辑,使URL在一行中,每个URL之间用\n字符分隔 这是到目前为止我的代码Php 如何循环所有文件夹并将其打开,读取放在每行末尾的文件,\n并使其成为单行,然后将其另存为新文件,php,loops,opendir,Php,Loops,Opendir,我有一个目录,其中只包含很多文件夹,每个文件夹都有一个包含URL的文本文件。我尝试创建一个php代码,在每个文件夹上打开该文本文件,并对其进行编辑,使URL在一行中,每个URL之间用\n字符分隔 这是到目前为止我的代码 $path = "localpath here"; $handle = opendir($path); while ($file = readdir($handle)) { if(substr($file,0,1) !="."){ $text = pre
$path = "localpath here";
$handle = opendir($path);
while ($file = readdir($handle)) {
if(substr($file,0,1) !="."){
$text = preg_replace('/\s+/', '', $file);
//echo $text."</br>";
$blast = fopen("$path/$text/$text.txt", 'r') or die("can't open file");
//echo $blast;
while (!feof($blast)) {
$members[] = fgets($blast);
//echo $members;
}
}
}
foreach($members as $x=> $order){
echo $order."</br>";
$string = trim(preg_replace('/\s+/', ' ', $order));
$linksonly = "write.txt";
$linksonlyHandle = fopen("$path/$text/$linksonly", 'a') or die("can't open file");
fwrite($linksonlyHandle,$string.'\n');
fclose($linksonlyHandle);
}
closedir($handle);
$path=“localpath here”;
$handle=opendir($path);
而($file=readdir($handle)){
if(substr($file,0,1)!=“){
$text=preg_replace('/\s+/',''.$file);
//回显$text.“”;
$blast=fopen($path/$text/$text.txt),'r')或die(“无法打开文件”);
//回声爆炸;
而(!feof($blast)){
$members[]=fgets($blast);
//echo$成员;
}
}
}
foreach($x=>$order形式的成员){
回显$order.“”;
$string=trim(preg_replace('/\s+/','.$order));
$linksonly=“write.txt”;
$linksonlyHandle=fopen($path/$text/$linksonly,'a')或die(“无法打开文件”);
fwrite($linksonlyHandle,$string.\n');
fclose($linksonlyHandle);
}
closedir($handle);
似乎希望$text在foreach()循环中是动态的,但它保留while()循环最后一次迭代的值
您可能希望为$text创建另一个数组,以便在foreach()循环中与具有相同索引的$member[]一起使用
$i=0;
$j=0;
如果我正确地理解了你想要实现的目标,那么你还有一些问题要解决。如果您要沿着目录树走,那么您可能需要创建一些函数并递归地执行。此外,您还需要检查从readdir获得的
$file
是一个文件还是另一个目录。您可以使用函数来执行此操作。如果它是一个目录,那么您需要在其中查找更多的文件(和目录)。这就是为什么使用递归函数是有帮助的
我通常会这样做:
function import_directory($directory)
{
$files = scandir($directory);
foreach ($files as $file)
{
if ($file == '.' || $file == '..')
continue;
if (is_dir($directory.'/'.$file))
$this->import_directory($directory.'/'.$file);
else
$this->import_file($directory.'/'.$file);
}
}
该示例使用scandir而不是readdir,但您可以理解这一点。它检查下一个“文件”是目录还是文件,如果它是目录,则再次调用相同的import\u directory函数。如果它是一个文件,那么你可以做任何你需要做的工作。在您的情况下,听起来像是读取文件的内容,稍微修改内容,然后将其写入另一个文件
要回答您的特定问题,即为什么只在最后一个文件夹中创建包含所有内容的文件,这是因为您在目录内容上执行完整的while循环(每次更新$members数组)。循环完成后,在foreach中只处理$members数组一次。为了使它更像您期望的那样工作,您需要在读取文件后,将处理$members数组(即文件内容)的foreach循环移动到main while循环中。或者更好,因为您一次读取一行文件的内容(使用fgets),所以可以在同一循环中将该行写入新文件(例如,读取一行,写入一行)
但当涉及到处理目录树时,我真的建议使用递归函数,因为它使它更简单,更容易理解-至少对我来说是这样 问题不太清楚。该代码应该做什么?
$linksonlyHandle = fopen("$path/{$text[$j]}/$linksonly", 'a') or die("can't open file");
$j++;
function import_directory($directory)
{
$files = scandir($directory);
foreach ($files as $file)
{
if ($file == '.' || $file == '..')
continue;
if (is_dir($directory.'/'.$file))
$this->import_directory($directory.'/'.$file);
else
$this->import_file($directory.'/'.$file);
}
}