Dan
Dan

Reputation: 517

count full listing of files in subdirectories php

I have this function I retrieve all the subfolders files (5000), from an specific directory /gifs ...and renames them (works well)

PROBLEM

I want to return a number of row li so it could be renamed with this number, I want any li have a correlative number that increases in one from file 0 to 5000. with this current function it returns correlative to the number of gifs per folder and then restars in the next folder...

$dir = '../gifs';

function listFolderFiles($dir){

  $results = new DirectoryIterator($dir);

  echo '<ul>';

  $count = 1;
  foreach($results as $fileInfo){

      if($fileInfo->isFile()){

        $oldFileName = $fileInfo->getPathname();
        $newFileName = $fileInfo->getPath() . '/' . $count . '.gif';

        //rename($oldFileName, $newFileName);

        echo '<li>' . 'File  Old: ' . $oldFileName . '  New: ' . $newFileName . '</li>';

        $count++;

      }

      if($fileInfo->isDir() && !$fileInfo->isDot()){

        echo '<li>' . 'Folder: ' . $fileInfo->getPathname() . '</li>';
        listFolderFiles($fileInfo->getPathname());

      }

  }

  echo '</ul>';



}


listFolderFiles($dir);

this is some of the files estructures, most of all have parenthesis on their names...

hugs
1 animaton (11).gif
2 animaton (12).gif
3 animaton (13).gif
4 animaton (17).gif
5 animaton (18).gif
6 animaton (19).gif
7 animaton (22).gif
8 animaton (23).gif
9 animaton (26).gif
10 animaton (27).gif
11 animaton (29).gif
12 animaton (31).gif
13 animaton (34).gif
14 animaton (35).gif
15 animaton (4).gif
16 animaton (41).gif
17 animaton (43).gif
18 animaton (46).gif
19 animaton (49).gif
20 animaton.gif
bored
21 animaton (1).gif
22 animaton (13).gif
23 animaton (17).gif
24 animaton (18).gif
25 animaton (19).gif
26 animaton (20).gif
27 animaton (25).gif
28 animaton (27).gif
29 animaton (29).gif
30 animaton (31).gif
31 animaton (33).gif
32 animaton (35).gif
33 animaton (36).gif
34 animaton (40).gif
35 animaton (41).gif
36 animaton (43).gif
37 animaton (45).gif
38 animaton (49).gif
39 animaton (5).gif
40 animaton (7).gif
41 animaton (9).gif
wink
42 animaton (11).gif
43 animaton (12).gif
 animaton (21).gif
 animaton (27).gif
 animaton (3).gif
 animaton (35).gif
 animaton (36).gif
 animaton (38).gif
 animaton (39).gif
 animaton (4).gif
 animaton (43).gif
 animaton (8).gif
sleepy
 animaton (11).gif
 animaton (12).gif
 animaton (13).gif
 animaton (14).gif
 animaton (16).gif
 animaton (17).gif
 animaton (19).gif
 animaton (20).gif
 animaton (21).gif
 animaton (22).gif
 animaton (24).gif
 animaton (26).gif
 animaton (27).gif
 animaton (29).gif
 animaton (3).gif
 animaton (32).gif
 animaton (34).gif
 animaton (35).gif
 animaton (36).gif
 animaton (37).gif
 animaton (38).gif
 animaton (39).gif
 animaton (4).gif
 animaton (40).gif
 animaton (41).gif
 animaton (45).gif
 animaton (46).gif
 animaton (49).gif
 animaton (5).gif
 animaton (6).gif
 animaton (9).gif
 animaton.gif
love
 animaton (1).gif
 animaton (11).gif
 animaton (13).gif
 animaton (14).gif
 animaton (17).gif
 animaton (18).gif
 animaton (20).gif
 animaton (21).gif
 animaton (24).gif
 animaton (25).gif
 animaton (27).gif
 animaton (28).gif
 animaton (29).gif
 animaton (38).gif
 animaton (39).gif
 animaton (4).gif
 animaton (40).gif
 animaton (41).gif
 animaton (44).gif
 animaton (48).gif
 animaton (5).gif
 animaton (7).gif
 animaton (9).gif
 animaton.gif
claps
 animaton (1).gif
 animaton (10).gif
 animaton (11).gif
 animaton (12).gif
 animaton (13).gif
 animaton (15).gif
 animaton (16).gif
 animaton (17).gif
 animaton (19).gif
 animaton (20).gif
 animaton (21).gif
 animaton (22).gif
 animaton (23).gif
 animaton (25).gif
 animaton (28).gif
 animaton (3).gif
 animaton (30).gif
 animaton (31).gif
 animaton (34).gif
 animaton (35).gif
 animaton (38).gif
 animaton (39).gif
 animaton (4).gif
 animaton (40).gif
 animaton (41).gif
 animaton (42).gif
 animaton (43).gif
 animaton (44).gif
 animaton (46).gif
 animaton (47).gif
 animaton (49).gif
 animaton (5).gif
 animaton (6).gif
 animaton (7).gif
 animaton (8).gif
scared
 animaton (1).gif
 animaton (10).gif
 animaton (12).gif
 animaton (13).gif
 animaton (15).gif
 animaton (16).gif
 animaton (17).gif
 animaton (18).gif
 animaton (21).gif
 animaton (24).gif
 animaton (33).gif
 animaton (39).gif
 animaton (40).gif
 animaton (46).gif
 animaton (6).gif

Upvotes: 0

Views: 42

Answers (1)

Mav
Mav

Reputation: 1190

Everytime the function is executed, $count is set to 1. To get around this, define $count outside the function and use global $count; inside your function to tell PHP that you want $count to refer to the variable outside the function's scope.

Here's the code

$dir = 'gifs';
$count = 1;

function listFolderFiles($dir){
  global $count;
  $results = new DirectoryIterator($dir);

  echo '<ul>';

  foreach($results as $fileInfo){

  if($fileInfo->isFile()){

    $oldFileName = $fileInfo->getPathname();
    $newFileName = $fileInfo->getPath() . '/' . $count . '.gif';

    //rename($oldFileName, $newFileName);

    echo '<li>' . 'File  Old: ' . $oldFileName . '  New: ' . $newFileName . '</li>';

    $count++;

  }

  if($fileInfo->isDir() && !$fileInfo->isDot()){

    echo '<li>' . 'Folder: ' . $fileInfo->getPathname() . '</li>';
    listFolderFiles($fileInfo->getPathname());

  }

  }

  echo '</ul>';



}


listFolderFiles($dir);

Upvotes: 1

Related Questions