php - How to loop all folders and open it, read file put \n on the end of each line and make it a single line and save it as a new file -


i have directory contains lot of folders , each of folder has text file contain urls im trying create php code open text file on each folder , edit , make urls in single line \n character separating each urls

this code far

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

seems want $text dynamic in foreach() loop, keeps value of last iteration of while() loop.

you may want create array $text used $member[] in foreach() loop identical index.

$i=0; $j=0; 

$members[$i] = fgets($blast); $text_array[$i] = $text; $i++; 

$linksonlyhandle = fopen("$path/{$text[$j]}/$linksonly", 'a') or die("can't open file"); $j++; 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -