PHP code example of popphp / pop-dir

1. Go to this page and download the library: Download popphp/pop-dir library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

popphp / pop-dir example snippets


use Pop\Dir\Dir;

$dir = new Dir('my-dir');

foreach ($dir->getFiles() as $file) {
    echo $file;
}

use Pop\Dir\Dir;

$dir = new Dir('my-dir', [
    'absolute'  => true,
    'recursive' => true
]);

foreach ($dir->getFiles() as $file) {
    echo $file;
}

$options = [
    'absolute'  => true,  // store the absolute, full path of the items in the directory
    'relative'  => false, // store the relative path of the items in the directory
    'recursive' => true,  // traverse the directory recursively
    'filesOnly' => false, // store only files in the object (and not other directories)
];

use Pop\Dir\Dir;

$dir = new Dir('my-dir');
$dir->setRecursive(true)
    ->setAbsolute(true);

use Pop\Dir\Dir;

$dir = new Dir('my-dir');

count($dir); // number of items found

// same as foreach ($dir->getFiles() as $file)
foreach ($dir as $file) {
    echo $file;
}

if (isset($dir['file1.txt'])) {
    echo $dir['file1.txt'];   // 'file1.txt'
    echo $dir[0];             // whatever the entry at index 0 is
}

$dir->fileExists('file1.txt'); // bool, same lookup as isset($dir['file1.txt'])

$dir->{0};             // same as $dir[0]
$dir->{'file1.txt'};   // same as $dir['file1.txt']
isset($dir->{'file1.txt'});

use Pop\Dir\Dir;

$dir = new Dir('my-dir', ['recursive' => true]);
print_r($dir->getTree());

use Pop\Dir\Dir;

$dir = new Dir('my-dir');
$dir->copyTo('/path/to/destination');

$dir->copyTo('/path/to/destination', false);

use Pop\Dir\Dir;

$dir = new Dir('my-dir');

$dir->deleteFile('file1.txt');
// or
unset($dir['file1.txt']);
// or
unset($dir->{'file1.txt'});

use Pop\Dir\Dir;

$dir = new Dir('my-dir');
$dir->emptyDir();

$dir->emptyDir(true);

$dir->emptyDir(false, null, true);

use Pop\Dir\Dir;
use Pop\Dir\Exception;

try {
    $dir = new Dir('/path/that/does-not-exist');
} catch (Exception $e) {
    echo $e->getMessage();
}