Jump to content

directory file list


rick645
Go to solution Solved by requinix,

Recommended Posts

dir$ pwd
/tmp/tmp.xWJxJWnltu/dir
dir$ tree
.
├── a
├── b
└── c
0 directories, 3 files
dir$ cat ../test.php 
<?php
var_dump(
    'OUTPUT1',
    new RecursiveDirectoryIterator(
        getcwd()
    )
);
var_dump(
    'OUTPUT2',
    iterator_to_array(
        new RecursiveDirectoryIterator(
            getcwd()
        )
    )
);
var_dump(
    'OUTPUT3',
    iterator_to_array(
        new RecursiveDirectoryIterator(
            getcwd(),
            FilesystemIterator::CURRENT_AS_PATHNAME
        )
    )
);
var_dump(
    'OUTPUT4',
    iterator_to_array(
        new RecursiveDirectoryIterator(
            getcwd(),
            FilesystemIterator::CURRENT_AS_PATHNAME .
            FilesystemIterator::SKIP_DOTS
        )
    )
);
dir$ php ../test.php 
/tmp/tmp.xWJxJWnltu/test.php:5:
string(7) "OUTPUT1"
/tmp/tmp.xWJxJWnltu/test.php:5:
class RecursiveDirectoryIterator#1 (4) {
  private $pathName =>
  string(25) "/tmp/tmp.xWJxJWnltu/dir/."
  private $fileName =>
  string(1) "."
  private $glob =>
  bool(false)
  private $subPathName =>
  string(0) ""
}
/tmp/tmp.xWJxJWnltu/test.php:12:
string(7) "OUTPUT2"
/tmp/tmp.xWJxJWnltu/test.php:12:
array(5) {
  '/tmp/tmp.xWJxJWnltu/dir/.' =>
  class SplFileInfo#3 (2) {
    private $pathName =>
    string(25) "/tmp/tmp.xWJxJWnltu/dir/."
    private $fileName =>
    string(1) "."
  }
  '/tmp/tmp.xWJxJWnltu/dir/c' =>
  class SplFileInfo#4 (2) {
    private $pathName =>
    string(25) "/tmp/tmp.xWJxJWnltu/dir/c"
    private $fileName =>
    string(1) "c"
  }
  '/tmp/tmp.xWJxJWnltu/dir/..' =>
  class SplFileInfo#5 (2) {
    private $pathName =>
    string(26) "/tmp/tmp.xWJxJWnltu/dir/.."
    private $fileName =>
    string(2) ".."
  }
  '/tmp/tmp.xWJxJWnltu/dir/b' =>
  class SplFileInfo#6 (2) {
    private $pathName =>
    string(25) "/tmp/tmp.xWJxJWnltu/dir/b"
    private $fileName =>
    string(1) "b"
  }
  '/tmp/tmp.xWJxJWnltu/dir/a' =>
  class SplFileInfo#7 (2) {
    private $pathName =>
    string(25) "/tmp/tmp.xWJxJWnltu/dir/a"
    private $fileName =>
    string(1) "a"
  }
}
/tmp/tmp.xWJxJWnltu/test.php:21:
string(7) "OUTPUT3"
/tmp/tmp.xWJxJWnltu/test.php:21:
array(5) {
  '/tmp/tmp.xWJxJWnltu/dir/.' =>
  string(25) "/tmp/tmp.xWJxJWnltu/dir/."
  '/tmp/tmp.xWJxJWnltu/dir/c' =>
  string(25) "/tmp/tmp.xWJxJWnltu/dir/c"
  '/tmp/tmp.xWJxJWnltu/dir/..' =>
  string(26) "/tmp/tmp.xWJxJWnltu/dir/.."
  '/tmp/tmp.xWJxJWnltu/dir/b' =>
  string(25) "/tmp/tmp.xWJxJWnltu/dir/b"
  '/tmp/tmp.xWJxJWnltu/dir/a' =>
  string(25) "/tmp/tmp.xWJxJWnltu/dir/a"
}
/tmp/tmp.xWJxJWnltu/test.php:31:
string(7) "OUTPUT4"
/tmp/tmp.xWJxJWnltu/test.php:31:
array(3) {
  '/tmp/tmp.xWJxJWnltu/dir/c' =>
  class SplFileInfo#5 (2) {
    private $pathName =>
    string(25) "/tmp/tmp.xWJxJWnltu/dir/c"
    private $fileName =>
    string(1) "c"
  }
  '/tmp/tmp.xWJxJWnltu/dir/b' =>
  class SplFileInfo#4 (2) {
    private $pathName =>
    string(25) "/tmp/tmp.xWJxJWnltu/dir/b"
    private $fileName =>
    string(1) "b"
  }
  '/tmp/tmp.xWJxJWnltu/dir/a' =>
  class SplFileInfo#3 (2) {
    private $pathName =>
    string(25) "/tmp/tmp.xWJxJWnltu/dir/a"
    private $fileName =>
    string(1) "a"
  }
}

The output that comes closest to my expectations is OUTPUT4 (everything else is to be discarded).
But actually I would like something like this

array(3) {
  [0] =>
  string(25) "/tmp/tmp.xWJxJWnltu/dir/c"
  [1] =>
  string(25) "/tmp/tmp.xWJxJWnltu/dir/b"
  [2] =>
  string(25) "/tmp/tmp.xWJxJWnltu/dir/a"
}

The simplest output!!!

Is it possible?

Link to comment
Share on other sites

For every problem, we always tend to look for the simplest solution
 

$solution1 = 2 * 5;

$solution2 = 2 + 2 + 2 + 2 + 2;

for ($i = 1; $i <= 5; $i++) {
    $solution3 = $i * 2 . PHP_EOL;
}

Which do you prefer?

Link to comment
Share on other sites

For every problem, we always tend to look for the simplest solution
 

$solution1 = 2 * 5;

$solution2 = 2 + 2 + 2 + 2 + 2;

for ($i = 1; $i <= 5; $i++) {
    $solution3 = $i * 2 . PHP_EOL;
}

Which do you prefer?

Link to comment
Share on other sites

Solution1, naturally because it is the simplest. (Although I did once have a holiday job working for a guy who would prefer solution2 as multiplication was beyond him)

But being a one-liner doesn't always make it the simplest ...

$solution4 = array_sum(array_map(fn($v) => $v / sin(M_PI/6), array_fill_keys(range(0,4), 1)));

 

Link to comment
Share on other sites

1 hour ago, rick645 said:

For every problem, we always tend to look for the simplest solution
 

$solution1 = 2 * 5;

$solution2 = 2 + 2 + 2 + 2 + 2;

for ($i = 1; $i <= 5; $i++) {
    $solution3 = $i * 2 . PHP_EOL;
}

Which do you prefer?

That's not a valid comparison. There is logic within the PHP engine to know what to do with the asterisk symbol. It was purpose built to do multiplication. If it did not exist and you wanted to be able to multiply arbitrary numbers you would have to build a function. For example, if the PHP engine knew how to add, but not to multiple here would be one way:

function product ($a, $b) {
	$product = 0;
	for ($i=0; $i<$a; $i++) { $product += $b; }
    return $product;
}

Using the asterisk is kind of like calling an internal function to do the multiplication. For your purposes, you can create a function to do what you need and call that function as a single line.

outputDirectory($rootDirectroy);

Your logic of a single line is the simplest solution is predicated on the assumption that there is a purpose built function for your needs. If not, one needs to be created. And, I will take readable, multi-line code over condensed code every time. It's a pain to go back to some old code that needs to be refactored and trying to decipher some complicated process that was condensed down to a few lines trying to figure out how it works. Give me separate lines for each step in the process with comments on what it is expected to do.

Link to comment
Share on other sites

On 5/2/2024 at 21:22, requinix said:

Le battute sono fantastiche e tutto il resto, ma vale tutto il tempo che ci hai dedicato finora? Un semplice ciclo foreach avrebbe potuto risolvere il problema ore fa.

since php doesn't offer anything native, I'd say this is the solution

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.