0

I have a directory that contains a lot of folders only and each of those folder has a text file that contain urls what Im trying to do is to create a php code that will open that text file on each that folder and will edit that and make that urls in a single line with \n character separating each urls

This is my code so 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 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);
denzuko
  • 1
  • 1

2 Answers2

0

Seems like you want $text to be dynamic in the foreach() loop, but it keeps value of the last iteration of the while() loop.

You may want to create another array for $text to be used with $member[] in the foreach() loop with an 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++;
David Houde
  • 4,835
  • 1
  • 20
  • 29
0

If I'm correctly understanding what you are trying to accomplish, you've got a few problems to work out. If you're going down a directory tree then you will probably want to create some functions and do it recursively. Also, you'll need to check if the $file you get from readdir is a file or another directory. You can do this using the is_dir function. If it is a directory, then you will need to look for more files (and directories) inside of it. This is why using recursive functions is helpful.

I usually do something like this:

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

That example uses scandir rather than readdir, but you can get the idea. It checks whether or not the next 'file' is a directory or a file, if it's a directory it calls the same import_directory function again. If it's a file then you can do whatever work you need to do. In your case it sounds like that is reading in the contents of the file, modifying the content slightly, and writing it out to another file.

To answer your specific question about why the file is only created in the last folder with all of the contents in it, that's because you do the complete while loop over the directory contents (updating the $members array each time). After that loop is done you process the $members array only one time in the foreach. To make it work more like you are expecting, you need to move the foreach loop that processes the $members array (i.e. the contents of the file) inside the main while loop, after the file has been read. Or better yet, since you are reading in the contents of the file one line at a time (using fgets), you could then write that line to your new file in the same loop (e.g. read a line, write a line).

But when it comes to processing a directory tree, I really recommend using a recursive function as it makes it simpler and easier to understand - at least for me!

phansen
  • 411
  • 3
  • 7