Rev 37 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 7 | ls | 1 | <?php |
| 2 | # ============================================================================= |
||
| 3 | # Create |
||
| 43 | - | 4 | # name : Andre Rikkert de Koe date: Apr 2011 |
| 7 | ls | 5 | # descr : filefind |
| 6 | # This is a demo of programming in the PHP language (CLI). |
||
| 7 | # Its part of a project to implement a program to create a directory |
||
| 8 | # tree similar to the output of the Unix find program. |
||
| 9 | # EUID : Any user who has read access to all the files being searched for. |
||
| 10 | # run : interactive |
||
| 11 | # |
||
| 12 | # Changes |
||
| 13 | # name : date: |
||
| 14 | # descr : short description |
||
| 15 | # |
||
| 16 | # ============================================================================= |
||
| 17 | |||
| 18 | # ============================================================================= |
||
| 19 | # Functions |
||
| 20 | # ============================================================================= |
||
| 21 | |||
| 37 | - | 22 | function Usage($progname ,$mesg) |
| 7 | ls | 23 | { |
| 37 | - | 24 | echo "Usage: $progname directory\n"; |
| 7 | ls | 25 | echo "$mesg\n"; |
| 26 | } |
||
| 27 | |||
| 28 | function traverse($dir) |
||
| 29 | { |
||
| 30 | $curdir = getcwd(); |
||
| 31 | chdir($dir); |
||
| 32 | $curdir2 = getcwd(); |
||
| 33 | $myDirectory = opendir("."); |
||
| 34 | # http://php.net/manual/fr/function.readdir.php |
||
| 35 | while (false !== ($file = readdir($myDirectory))) |
||
| 36 | { |
||
| 37 | $pfile = $curdir2 . "/" . $file; |
||
| 38 | if (is_dir($file) && !is_link($file)) |
||
| 39 | { |
||
| 40 | if ($file != ".." && $file != ".") |
||
| 41 | { |
||
| 42 | echo "$pfile\n"; |
||
| 43 | traverse($file); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | else |
||
| 47 | { |
||
| 48 | if (is_file($file) || is_link($file)) |
||
| 49 | echo "$pfile\n"; |
||
| 50 | else |
||
| 51 | echo "WARNING: Could not process file $pfile\n"; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | chdir($curdir); |
||
| 55 | |||
| 56 | } |
||
| 57 | |||
| 58 | function start_traverse($directory) |
||
| 59 | { |
||
| 60 | echo "$directory\n"; |
||
| 61 | traverse($directory); |
||
| 62 | } |
||
| 63 | |||
| 64 | # ============================================================================= |
||
| 65 | # MAIN |
||
| 66 | # ============================================================================= |
||
| 67 | |||
| 68 | $progname = $argv[0]; |
||
| 69 | |||
| 70 | if ( $argc == 2 ) |
||
| 71 | { |
||
| 72 | $directory = $argv[1]; |
||
| 73 | if (is_dir($directory)) |
||
| 74 | start_traverse($directory); |
||
| 75 | else |
||
| 37 | - | 76 | Usage($progname, "ERROR: Not a directory : $directory"); |
| 7 | ls | 77 | } |
| 78 | else |
||
| 79 | Usage($progname, "ERROR: 1 argument expected"); |
||
| 80 | |||
| 81 | ?> |