পিএইচপি পুনরাবৃত্তি ফাংশন সহ একটি ডিরেক্টরিতে সমস্ত ফাইল এবং ফোল্ডার তালিকাবদ্ধ করুন


87

আমি একটি ডিরেক্টরিতে থাকা সমস্ত ফাইলের মধ্যে দিয়ে যাওয়ার চেষ্টা করছি, এবং যদি কোনও ডিরেক্টরি থাকে তবে এর সমস্ত ফাইল এবং তাই দিয়ে যাওয়া যতক্ষণ না আরও কোনও ডিরেক্টরি না যায়। প্রতিটি এবং প্রতিটি প্রক্রিয়াজাত আইটেম নীচের ফাংশন একটি ফলাফল অ্যারে যোগ করা হবে। এটি কাজ করছে না যদিও আমি নিশ্চিত যে আমি কী করতে পারি / আমি কী ভুল করেছিলাম তা নিশ্চিত নই, তবে নীচের কোডটি প্রসেস করা হলে ব্রাউজারটি অত্যন্ত ধীর গতিতে চলে যায়, কোনও সাহায্যের প্রশংসা করা হয়, ধন্যবাদ!

কোড:

    function getDirContents($dir){
        $results = array();
        $files = scandir($dir);

            foreach($files as $key => $value){
                if(!is_dir($dir. DIRECTORY_SEPARATOR .$value)){
                    $results[] = $value;
                } else if(is_dir($dir. DIRECTORY_SEPARATOR .$value)) {
                    $results[] = $value;
                    getDirContents($dir. DIRECTORY_SEPARATOR .$value);
                }
            }
    }

    print_r(getDirContents('/xampp/htdocs/WORK'));

7
RecursiveDirectoryIterator
u_mulder

@ ব্যবহারকারী 3412869 আপনার কাছে থাকলে .বা ফাংশনটিতে কল করবেন না ..। আমার উত্তর দেখুন।
এ 312

উত্তর:


152

ডিরেক্টরিতে সমস্ত ফাইল এবং ফোল্ডার পান, যখন আপনার থাকে .বা তখন ফাংশন কল করবেন না ..

তোমার গোপন সংকেত :

<?php
function getDirContents($dir, &$results = array()) {
    $files = scandir($dir);

    foreach ($files as $key => $value) {
        $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
        if (!is_dir($path)) {
            $results[] = $path;
        } else if ($value != "." && $value != "..") {
            getDirContents($path, $results);
            $results[] = $path;
        }
    }

    return $results;
}

var_dump(getDirContents('/xampp/htdocs/WORK'));

আউটপুট (উদাহরণ):

array (size=12)
  0 => string '/xampp/htdocs/WORK/iframe.html' (length=30)
  1 => string '/xampp/htdocs/WORK/index.html' (length=29)
  2 => string '/xampp/htdocs/WORK/js' (length=21)
  3 => string '/xampp/htdocs/WORK/js/btwn.js' (length=29)
  4 => string '/xampp/htdocs/WORK/js/qunit' (length=27)
  5 => string '/xampp/htdocs/WORK/js/qunit/qunit.css' (length=37)
  6 => string '/xampp/htdocs/WORK/js/qunit/qunit.js' (length=36)
  7 => string '/xampp/htdocs/WORK/js/unit-test.js' (length=34)
  8 => string '/xampp/htdocs/WORK/xxxxx.js' (length=30)
  9 => string '/xampp/htdocs/WORK/plane.png' (length=28)
  10 => string '/xampp/htdocs/WORK/qunit.html' (length=29)
  11 => string '/xampp/htdocs/WORK/styles.less' (length=30)

ফলাফল অ্যারেতে প্রতিটি ফোল্ডারটি তৈরি করা কি সম্ভব হবে, সমস্ত বাচ্চাদের ফাইলের নিজস্ব অ্যারে কি রয়েছে?
ব্যবহারকারী 3412869

10 দ্বারা লাইনটি প্রতিস্থাপন করুন:getDirContents($path, $results[$path]);
এ 312

4
scandir()কর্মক্ষমতা সমালোচনামূলক হলে ব্যবহার করা কোনও ভাল ধারণার মতো লাগে না। আরও ভাল বিকল্পটি হ'ল RecursiveDirectoryIterator( php.net/manual/en/class.recursusedirectoryiterator.php )
মুগোমা জে ওকোম্বা

ডিরেক্টরিটি ফাঁকা থাকলে উপরের ফাংশনটি মোট গণনা 1
গোলাম আব্বাস

ব্যবহার realpath()করে একই ডিরেক্টরিতে প্রতীকী লিঙ্কগুলির টার্গেটের নাম দেওয়া হবে। উদাহরণস্বরূপ, একটি লিনাক্স মেশিনে "/ usr / lib64" উদাহরণ ব্যবহার করুন।
ম্যাটবিয়ানকো

105
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('path/to/folder'));

$files = array(); 

foreach ($rii as $file) {

    if ($file->isDir()){ 
        continue;
    }

    $files[] = $file->getPathname(); 

}



var_dump($files);

এটি আপনার সাথে সমস্ত ফাইল আনবে পাথ।


বিল্ট ইন অবজেক্ট ছাড়া এটি করার কোনও উপায় নেই?
ব্যবহারকারী 3412869

4
অথবা আপনি কি শর্ত বিপরীত করতে পারেন: if (!$file->isDir()) $files[] = $file->getPathname();। একটি লাইন সংরক্ষণ করতে।
এ 312


$Regex = new RegexIterator($rii, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
পূর্বাভাসটি এতে

@ রাজাভানগ্রিগোর আমি নিশ্চিত নন যে এটি কীভাবে নন .এবং ..ডিরেক্টরিতে সহায়তা করে । আপনি এখনও তাদের ফিল্টার প্রয়োজন হবে না?
War10ck

26

এটির সংক্ষিপ্ত সংস্করণ:

function getDirContents($path) {
    $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));

    $files = array(); 
    foreach ($rii as $file)
        if (!$file->isDir())
            $files[] = $file->getPathname();

    return $files;
}

var_dump(getDirContents($path));

7
ডাউনভোট হিসাবে এটি আসলে কোনও উন্নতি নয়। এটি ঠিক একই উত্তরটি কিছুটা ভিন্নভাবে লেখা। এটি শৈলীর প্রশ্নে নেমে আসে। গার্ড ক্লজ (জ্যাঙ্কোকার সংস্করণ) একেবারে ঠিক আছে।
মার্শহাউস

4
জ্যাঙ্কোকার সংস্করণটি ভাল, আপনার উত্তরটির সত্যই প্রয়োজন নেই, তাঁর পক্ষে একটি মন্তব্য যথেষ্ট ছিল। এটি কেবল কোডিং শৈলীতে নেমে আসে।
অগওয়া

8

ডিরেক্টরিতে ফিল্টার (২ য় আর্গুমেন্ট) এবং ফোল্ডার সহ সমস্ত ফাইল পান , যখন আপনার থাকে .বা তখন ফাংশন কল করবেন না ..

তোমার গোপন সংকেত :

<?php
function getDirContents($dir, $filter = '', &$results = array()) {
    $files = scandir($dir);

    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value); 

        if(!is_dir($path)) {
            if(empty($filter) || preg_match($filter, $path)) $results[] = $path;
        } elseif($value != "." && $value != "..") {
            getDirContents($path, $filter, $results);
        }
    }

    return $results;
} 

// Simple Call: List all files
var_dump(getDirContents('/xampp/htdocs/WORK'));

// Regex Call: List php files only
var_dump(getDirContents('/xampp/htdocs/WORK', '/\.php$/'));

আউটপুট (উদাহরণ):

// Simple Call
array(13) {
  [0]=> string(69) "/xampp/htdocs/WORK.htaccess"
  [1]=> string(73) "/xampp/htdocs/WORKConverter.php"
  [2]=> string(69) "/xampp/htdocs/WORKEvent.php"
  [3]=> string(70) "/xampp/htdocs/WORKdefault_filter.json"
  [4]=> string(68) "/xampp/htdocs/WORKdefault_filter.xml"
  [5]=> string(80) "/xampp/htdocs/WORKCaching/ApcCache.php"
  [6]=> string(84) "/xampp/htdocs/WORKCaching/CacheFactory.php"
}

// Regex Call
array(13) {
  [0]=> string(69) "/xampp/htdocs/WORKEvent.php"
  [1]=> string(73) "/xampp/htdocs/WORKConverter.php"
  [2]=> string(80) "/xampp/htdocs/WORKCaching/ApcCache.php"
  [3]=> string(84) "/xampp/htdocs/WORKCaching/CacheFactory.php"
}

জেমস ক্যামেরনের প্রস্তাব।


6

কুরুচিপূর্ণ "ফরচ" নিয়ন্ত্রণ কাঠামো ছাড়াই আমার প্রস্তাব is

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$allFiles = array_filter(iterator_to_array($iterator), function($file) {
    return $file->isFile();
});

আপনি কেবল ফাইলপথটিই বের করতে চাইতে পারেন, যা আপনি এটির মাধ্যমে করতে পারেন:

array_keys($allFiles);

কোডের 4 টি লাইন, তবে লুপ বা কিছু ব্যবহার করার চেয়ে আরও সোজা এগিয়ে।


4
একই সাথে মেমরিতে সমস্ত ফাইল এবং ডিরেক্টরি লোড করা এড়াতে CallbackFilterIteratorআপনি পরে একটি লুপও করতে পারেন যা ব্যবহার করতে পারেন:$allFilesIterator = new CallbackFilterIterator($iterator, function(SplFileInfo $fileInfo) { return $fileInfo->isFile(); });
অ্যাড ম্যাথিজসেন

6

আপনি যদি লুকানো ফাইল এবং ডিরেক্টরি উপেক্ষা করে অ্যারে হিসাবে ডিরেক্টরি সামগ্রী পেতে চান তবে এটি সহায়তা করতে পারে help

function dir_tree($dir_path)
{
    $rdi = new \RecursiveDirectoryIterator($dir_path);

    $rii = new \RecursiveIteratorIterator($rdi);

    $tree = [];

    foreach ($rii as $splFileInfo) {
        $file_name = $splFileInfo->getFilename();

        // Skip hidden files and directories.
        if ($file_name[0] === '.') {
            continue;
        }

        $path = $splFileInfo->isDir() ? array($file_name => array()) : array($file_name);

        for ($depth = $rii->getDepth() - 1; $depth >= 0; $depth--) {
            $path = array($rii->getSubIterator($depth)->current()->getFilename() => $path);
        }

        $tree = array_merge_recursive($tree, $path);
    }

    return $tree;
}

ফলাফল কিছু হবে;

dir_tree(__DIR__.'/public');

[
    'css' => [
        'style.css',
        'style.min.css',
    ],
    'js' => [
        'script.js',
        'script.min.js',
    ],
    'favicon.ico',
]

উৎস


3

আমি এখানে যা এলাম তা এখানে কোডের বেশি লাইন নয়

function show_files($start) {
    $contents = scandir($start);
    array_splice($contents, 0,2);
    echo "<ul>";
    foreach ( $contents as $item ) {
        if ( is_dir("$start/$item") && (substr($item, 0,1) != '.') ) {
            echo "<li>$item</li>";
            show_files("$start/$item");
        } else {
            echo "<li>$item</li>";
        }
    }
    echo "</ul>";
}

show_files('./');

এটি কিছু ভালো ফলাফল

..idea
.add.php
.add_task.php
.helpers
 .countries.php
.mysqli_connect.php
.sort.php
.test.js
.test.php
.view_tasks.php

** বিন্দুগুলি আনঅর্ডারার্ড তালিকার বিন্দু।

আশাকরি এটা সাহায্য করবে.


যেহেতু আপনি প্রশ্ন উত্থাপিত হওয়ার দু'বছর পরে আপনি একটি উত্তর যুক্ত করেছেন: আমি কেন স্বীকৃত উত্তর বা আইডেম্যাটিক রিকার্সিভ ডিরেক্টরআইট্রেটর সমাধানের মাধ্যমে আপনার ব্যবহার করতে চাই?
গর্ডন

আমি কয়েক মাস আগে পিএইচপি শিখতে শুরু করেছি। আমি এই প্রশ্নের একটি সমাধান অনুসন্ধান করেছি কিন্তু আমার নিজের সমাধানটিও সাথে নিয়ে আসার চেষ্টা করেছি। আমি এটি ভেবে পোস্ট করেছি যে সম্ভবত আমার সমাধান যদি কাউকে সহায়তা করে।
কুশিক দাস

4
আইআইএস উইন্ডোজ 2012 সার্ভারের ভিত্তিতে পিএইচপি ওয়েবসাইটে কেবলমাত্র আমার জন্যই কাজ করেছেন
মেলোম্যান

3

এখানে হর্সের উত্তরের একটি পরিবর্তিত সংস্করণ রয়েছে, এটি আমার ক্ষেত্রে কিছুটা ভাল কাজ করে, কারণ এটি যেভাবে বেজ ডিরেক্টরিটি যায় তেমন পাস করে, এবং এটি একটি পুনরাবৃত্ত সুইচ রয়েছে যা মিথ্যাতে সেট করা যেতে পারে যা কার্যকর। আউটপুটটিকে আরও পঠনযোগ্য করে তোলার জন্য, আমি ফাইল এবং সাব-ডাইরেক্টরি ফাইলগুলি পৃথক করেছি, তাই ফাইলগুলি প্রথমে সাব-ডাইরেক্টরি ফাইল যুক্ত করা হয় (আমার অর্থের ফলাফল দেখুন))

আমি আশেপাশে কয়েকটি অন্যান্য পদ্ধতি এবং পরামর্শ চেষ্টা করেছি এবং এটিই আমার শেষ হয়েছিল। আমি অন্য কাজ পদ্ধতি ইতিমধ্যে যে অনুরূপ হয়েছিল, কিন্তু ব্যর্থ যেখানে কোন ফাইল সঙ্গে একটি সাব ছিল করলো কিন্তু যে সাব একটি subsubdirectory ছিল সঙ্গে ফাইল, এটা ফাইলের জন্য subsubdirectory স্ক্যান করা হয়নি - তাই কিছু উত্তর পরীক্ষিত করা প্রয়োজন হতে পারে সে ক্ষেত্রে।) ... যাইহোক ভেবেছিল কেউ যদি এখানে দেখছে তবে আমি আমার সংস্করণটি এখানে পোস্ট করব ...

function get_filelist_as_array($dir, $recursive = true, $basedir = '', $include_dirs = false) {
    if ($dir == '') {return array();} else {$results = array(); $subresults = array();}
    if (!is_dir($dir)) {$dir = dirname($dir);} // so a files path can be sent
    if ($basedir == '') {$basedir = realpath($dir).DIRECTORY_SEPARATOR;}

    $files = scandir($dir);
    foreach ($files as $key => $value){
        if ( ($value != '.') && ($value != '..') ) {
            $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
            if (is_dir($path)) {
                // optionally include directories in file list
                if ($include_dirs) {$subresults[] = str_replace($basedir, '', $path);}
                // optionally get file list for all subdirectories
                if ($recursive) {
                    $subdirresults = get_filelist_as_array($path, $recursive, $basedir, $include_dirs);
                    $results = array_merge($results, $subdirresults);
                }
            } else {
                // strip basedir and add to subarray to separate file list
                $subresults[] = str_replace($basedir, '', $path);
            }
        }
    }
    // merge the subarray to give the list of files then subdirectory files
    if (count($subresults) > 0) {$results = array_merge($subresults, $results);}
    return $results;
}

আমি মনে করি একটি জিনিস এটিকে কল করার সময় এই ফাংশনটির কোনও $ ভিত্তিক মান পাস না করার বিষয়ে সতর্কতা অবলম্বন করা উচিত ... বেশিরভাগ ক্ষেত্রে কেবল $ দির পাস করুন (বা একটি ফাইলপথ পাস করা এখন খুব কার্যকর হবে) এবং optionচ্ছিকভাবে মিথ্যা হিসাবে যদি পুনরাবৃত্তি হয় এবং প্রয়োজন। ফলাফল:

[0] => demo-image.png
[1] => filelist.php
[2] => tile.png
[3] => 2015\header.png
[4] => 2015\08\background.jpg

উপভোগ করুন! ঠিক আছে, প্রোগ্রামটিতে ফিরে আমি আসলে এটি ব্যবহার করছি ...

আপডেট করুন ফাইল তালিকায় ডিরেক্টরি অন্তর্ভুক্ত করার জন্য অতিরিক্ত যুক্তি যুক্ত করা হয়েছে বা নেই (অন্যান্য যুক্তিগুলি মনে রাখার জন্য এটি ব্যবহার করতে হবে be) উদাহরণস্বরূপ।

$results = get_filelist_as_array($dir, true, '', true);


ধন্যবাদ তবে এই ফাংশনটি ডিরেক্টরিগুলির তালিকা করে না। কেবল ফাইলগুলি
ডেনিজ পোরসুক

@ ডেনিজপোরসুক ভাল বাছাই করা উচিত, প্রশ্নটিতে অবশ্যই এটি মিস করে গেছেন। ডিরেক্টরিগুলি অন্তর্ভুক্ত করার জন্য আমি একটি alচ্ছিক যুক্তি যুক্ত করেছি। :-)
মজিক

2

এই সমাধানটি আমার পক্ষে কাজ করেছে। RecursiveIteratorIterator সমস্ত ডিরেক্টরি এবং ফাইলগুলি পুনরাবৃত্তিতে তবে অকার্যকরভাবে তালিকাভুক্ত করে। প্রোগ্রামটি তালিকাটি ফিল্টার করে এবং এটি সাজায়।

আমি নিশ্চিত যে এই সংক্ষিপ্ত লেখার একটি উপায় আছে; এটি উন্নত করতে নির্দ্বিধায় এটি কেবল একটি কোড স্নিপেট। আপনি এটি আপনার উদ্দেশ্যগুলিতে জ্বালাতন করতে চাইতে পারেন।

<?php

$path = '/pth/to/your/directories/and/files';
// an unsorted array of dirs & files
$files_dirs = iterator_to_array( new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),RecursiveIteratorIterator::SELF_FIRST) );

echo '<html><body><pre>';
// create a new associative multi-dimensional array with dirs as keys and their files
$dirs_files = array();
foreach($files_dirs as $dir){
 if(is_dir($dir) AND preg_match('/\/\.$/',$dir)){
  $d = preg_replace('/\/\.$/','',$dir);
  $dirs_files[$d] = array();
  foreach($files_dirs as $file){
   if(is_file($file) AND $d == dirname($file)){
    $f = basename($file);
    $dirs_files[$d][] = $f;
   }
  }
 }
}
//print_r($dirs_files);

// sort dirs
ksort($dirs_files);

foreach($dirs_files as $dir => $files){
 $c = substr_count($dir,'/');
 echo  str_pad(' ',$c,' ', STR_PAD_LEFT)."$dir\n";
 // sort files
 asort($files);
 foreach($files as $file){
  echo str_pad(' ',$c,' ', STR_PAD_LEFT)."|_$file\n";
 }
}
echo '</pre></body></html>';

?>

2

@ এ -31২ এর সমাধান মেমোরির সমস্যার কারণ হতে পারে কারণ /xampp/htdocs/WORKএতে প্রচুর ফাইল এবং ফোল্ডার রয়েছে তবে এটি বিশাল অ্যারে তৈরি করতে পারে ।

আপনার যদি পিএইচপি 7 থাকে তবে আপনি জেনারেটরগুলি ব্যবহার করতে পারেন এবং এই জাতীয় PHP এর স্মৃতিটিকে অনুকূল করতে পারেন :

function getDirContents($dir) {
    $files = scandir($dir);
    foreach($files as $key => $value){

        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {
            yield $path;

        } else if($value != "." && $value != "..") {
           yield from getDirContents($path);
           yield $path;
        }
    }
}

foreach(getDirContents('/xampp/htdocs/WORK') as $value) {
    echo $value."\n";
}

থেকে ফলন


1

এটি প্রদত্ত ডিরেক্টরিতে সমস্ত ফাইলের পুরো পথটি মুদ্রণ করবে, আপনি অন্যান্য কলব্যাক ফাংশনগুলি পুনরাবৃত্তির সাথেও পাস করতে পারবেন।

function printFunc($path){
    echo $path."<br>";
}

function recursiveDir($path, $fileFunc, $dirFunc){
    $openDir = opendir($path);
    while (($file = readdir($openDir)) !== false) {
        $fullFilePath = realpath("$path/$file");
        if ($file[0] != ".") {
            if (is_file($fullFilePath)){
                if (is_callable($fileFunc)){
                    $fileFunc($fullFilePath);
                }
            } else {
                if (is_callable($dirFunc)){
                    $dirFunc($fullFilePath);
                }
                recursiveDir($fullFilePath, $fileFunc, $dirFunc);
            }
        }
    }
}

recursiveDir($dirToScan, 'printFunc', 'printFunc');

বা:realpath("$path/$file");
এ 312

1

এটি মজিকের উত্তরের সামান্য পরিবর্তন ification
আমি ঠিক ফাংশন দ্বারা ফিরে অ্যারে কাঠামো পরিবর্তন।

থেকে:

array() => {
    [0] => "test/test.txt"
}

প্রতি:

array() => {
    'test/test.txt' => "test.txt"
}

/**
 * @param string $dir
 * @param bool   $recursive
 * @param string $basedir
 *
 * @return array
 */
function getFileListAsArray(string $dir, bool $recursive = true, string $basedir = ''): array {
    if ($dir == '') {
        return array();
    } else {
        $results = array();
        $subresults = array();
    }
    if (!is_dir($dir)) {
        $dir = dirname($dir);
    } // so a files path can be sent
    if ($basedir == '') {
        $basedir = realpath($dir) . DIRECTORY_SEPARATOR;
    }

    $files = scandir($dir);
    foreach ($files as $key => $value) {
        if (($value != '.') && ($value != '..')) {
            $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
            if (is_dir($path)) { // do not combine with the next line or..
                if ($recursive) { // ..non-recursive list will include subdirs
                    $subdirresults = self::getFileListAsArray($path, $recursive, $basedir);
                    $results = array_merge($results, $subdirresults);
                }
            } else { // strip basedir and add to subarray to separate file list
                $subresults[str_replace($basedir, '', $path)] = $value;
            }
        }
    }
    // merge the subarray to give the list of files then subdirectory files
    if (count($subresults) > 0) {
        $results = array_merge($subresults, $results);
    }
    return $results;
}

আমার মতো হুবহু একই প্রত্যাশিত ফলাফল প্রাপ্তদের জন্য সহায়তা করতে পারে।


1

ফোল্ডারগুলির চেয়ে প্রথমে যাদের তালিকা ফাইল দরকার (বর্ণানুক্রমিক পুরানো)।

নিম্নলিখিত ফাংশন ব্যবহার করতে পারেন। এটি স্ব-কলিং ফাংশন নয়। সুতরাং আপনার ডিরেক্টরি তালিকা, ডিরেক্টরি প্রদর্শন, ফাইল তালিকা এবং পৃথক অ্যারে হিসাবে ফোল্ডার তালিকা থাকবে।

আমি এর জন্য দু'দিন ব্যয় করেছি এবং চাই না যে কেউ এর জন্যও তার সময় নষ্ট করবে, আশা কাউকে সাহায্য করবে।

function dirlist($dir){
    if(!file_exists($dir)){ return $dir.' does not exists'; }
    $list = array('path' => $dir, 'dirview' => array(), 'dirlist' => array(), 'files' => array(), 'folders' => array());

    $dirs = array($dir);
    while(null !== ($dir = array_pop($dirs))){
        if($dh = opendir($dir)){
            while(false !== ($file = readdir($dh))){
                if($file == '.' || $file == '..') continue;
                $path = $dir.DIRECTORY_SEPARATOR.$file;
                $list['dirlist_natural'][] = $path;
                if(is_dir($path)){
                    $list['dirview'][$dir]['folders'][] = $path;
                    // Bos klasorler while icerisine tekrar girmeyecektir. Klasorun oldugundan emin olalım.
                    if(!isset($list['dirview'][$path])){ $list['dirview'][$path] = array(); }
                    $dirs[] = $path;
                    //if($path == 'D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-content\upgrade'){ press($path); press($list['dirview']); die; }
                }
                else{
                    $list['dirview'][$dir]['files'][] = $path;
                }
            }
            closedir($dh);
        }
    }

    // if(!empty($dirlist['dirlist_natural']))  sort($dirlist['dirlist_natural'], SORT_LOCALE_STRING); // delete safe ama gerek kalmadı.

    if(!empty($list['dirview'])) ksort($list['dirview']);

    // Dosyaları dogru sıralama yaptırıyoruz. Deniz P. - info[at]netinial.com
    foreach($list['dirview'] as $path => $file){
        if(isset($file['files'])){
            $list['dirlist'][] = $path;
            $list['files'] = array_merge($list['files'], $file['files']);
            $list['dirlist'] = array_merge($list['dirlist'], $file['files']);
        }
        // Add empty folders to the list
        if(is_dir($path) && array_search($path, $list['dirlist']) === false){
            $list['dirlist'][] = $path;
        }
        if(isset($file['folders'])){
            $list['folders'] = array_merge($list['folders'], $file['folders']);
        }
    }

    //press(array_diff($list['dirlist_natural'], $list['dirlist'])); press($list['dirview']); die;

    return $list;
}

এই জাতীয় কিছু আউটপুট হবে।

[D:\Xampp\htdocs\exclusiveyachtcharter.localhost] => Array
                (
                    [files] => Array
                        (
                            [0] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\.htaccess
                            [1] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\index.php
                            [2] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\license.txt
                            [3] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\php.php
                            [4] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\readme.html
                            [5] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-activate.php
                            [6] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-blog-header.php
                            [7] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-comments-post.php
                            [8] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-config-sample.php
                            [9] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-config.php
                            [10] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-cron.php
                            [11] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-links-opml.php
                            [12] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-load.php
                            [13] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-login.php
                            [14] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-mail.php
                            [15] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-settings.php
                            [16] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-signup.php
                            [17] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-trackback.php
                            [18] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\xmlrpc.php
                        )

                    [folders] => Array
                        (
                            [0] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\exclusiv_excluwlsql
                            [1] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-admin
                            [2] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-content
                            [3] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-includes
                        )

                )

dirview আউটপুট

    [dirview] => Array
        (
            [0] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\.htaccess
            [1] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\index.php
            [2] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\license.txt
            [3] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\php.php
            [4] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\readme.html
            [5] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-activate.php
            [6] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-blog-header.php
            [7] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-comments-post.php
            [8] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-config-sample.php
            [9] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-config.php
            [10] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-cron.php
            [11] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-links-opml.php
            [12] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-load.php
            [13] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-login.php
            [14] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-mail.php
            [15] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-settings.php
            [16] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-signup.php
            [17] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\wp-trackback.php
            [18] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\xmlrpc.php
            [19] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost
            [20] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\exclusiv_excluwlsql\exclusiv_excluwl.sql
            [21] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\exclusiv_excluwlsql\exclusiv_excluwl.sql.zip
            [22] => D:\Xampp\htdocs\exclusiveyachtcharter.localhost\exclusiv_excluwlsql
)

1

আপেক্ষিক পাথ বিকল্প যুক্ত করুন:

function getDirContents($dir, $relativePath = false)
{
    $fileList = array();
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
    foreach ($iterator as $file) {
        if ($file->isDir()) continue;
        $path = $file->getPathname();
        if ($relativePath) {
            $path = str_replace($dir, '', $path);
            $path = ltrim($path, '/\\');
        }
        $fileList[] = $path;
    }
    return $fileList;
}

print_r(getDirContents('/path/to/dir'));

print_r(getDirContents('/path/to/dir', true));

আউটপুট:

Array
(
    [0] => /path/to/dir/test1.html
    [1] => /path/to/dir/test.html
    [2] => /path/to/dir/index.php
)

Array
(
    [0] => test1.html
    [1] => test.html
    [2] => index.php
)

0

আমারটা এখানে :

function recScan( $mainDir, $allData = array() ) 
{ 
// hide files 
$hidefiles = array( 
".", 
"..", 
".htaccess", 
".htpasswd", 
"index.php", 
"php.ini", 
"error_log" ) ; 

//start reading directory 
$dirContent = scandir( $mainDir ) ; 

foreach ( $dirContent as $key => $content ) 
{ 
$path = $mainDir . '/' . $content ; 

// if is readable / file 
if ( ! in_array( $content, $hidefiles ) ) 
{ 
if ( is_file( $path ) && is_readable( $path ) ) 
{ 
$allData[] = $path ; 
} 

// if is readable / directory 
// Beware ! recursive scan eats ressources ! 
else 
if ( is_dir( $path ) && is_readable( $path ) ) 
{ 
/*recursive*/ 
$allData = recScan( $path, $allData ) ; 
} 
} 
} 

return $allData ; 
}  

0

এখানে আমি তার জন্য উদাহরণ আছে

পিএইচপি পুনরাবৃত্তি ফাংশন সহ একটি ডিরেক্টরি সিএসভি (ফাইল) পঠিত সমস্ত ফাইল এবং ফোল্ডার তালিকাবদ্ধ করুন

<?php

/** List all the files and folders in a Directory csv(file) read with PHP recursive function */
function getDirContents($dir, &$results = array()){
    $files = scandir($dir);

    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {
            $results[] = $path;
        } else if($value != "." && $value != "..") {
            getDirContents($path, $results);
            //$results[] = $path;
        }
    }

    return $results;
}





$files = getDirContents('/xampp/htdocs/medifree/lab');//here folder name where your folders and it's csvfile;


foreach($files as $file){
$csv_file =$file;
$foldername =  explode(DIRECTORY_SEPARATOR,$file);
//using this get your folder name (explode your path);
print_r($foldername);

if (($handle = fopen($csv_file, "r")) !== FALSE) {

fgetcsv($handle); 
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$col[$c] = $data[$c];
}
}
fclose($handle);
}

}

?>

http://myphpinformation.blogspot.in/2016/05/list-all-files-and-folders-in-directory-csv-file-read-with-php-recursive.html


0

ফলাফল অ্যারে ফোল্ডারগুলি এড়াতে আমি একটি চেক পুনরাবৃত্তির মাধ্যমে হর্স সুয়েটের ভাল কোড দিয়ে উন্নতি করেছি:

ফাংশন getDirContents (ir দির, & $ ফলাফল = অ্যারে ()) {

    $ ফাইলগুলি = স্ক্যান্ডির (ir দির);

    foreach ($ কী => $ মান হিসাবে ফাইল) {
        $ পথ = রিয়েলপথ (ir dir.DIRECTORY_SEPARATOR $ মান);
        যদি (is_dir ($ পথ) == মিথ্যা) {
            $ ফলাফল [] = $ পথ;
        }
        অন্যথায় যদি ($ মান! = "।" && $ মান! = "..") {
            getDirContents ($ পথ, $ ফলাফল);
            যদি (is_dir ($ পথ) == মিথ্যা) {
                $ ফলাফল [] = $ পথ;
            }   
        }
    }
    প্রত্যাবর্তন $ ফলাফল;

}

0

উপরের এক উত্তরের উন্নত / বর্ধিত সংস্করণ সাধারণ ব্যবহারের ক্ষেত্রে অনুলিপি এবং পেস্ট ফাংশনের জন্য প্রস্তুত :

function getDirContents(string $dir, int $onlyFiles = 0, string $excludeRegex = '~/\.git/~', int $maxDepth = -1): array {
    $results = [];
    $scanAll = scandir($dir);
    sort($scanAll);
    $scanDirs = []; $scanFiles = [];
    foreach($scanAll as $fName){
        if ($fName === '.' || $fName === '..') { continue; }
        $fPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($dir . '/' . $fName));
        if (strlen($excludeRegex) > 0 && preg_match($excludeRegex, $fPath . (is_dir($fPath) ? '/' : ''))) { continue; }
        if (is_dir($fPath)) {
            $scanDirs[] = $fPath;
        } elseif ($onlyFiles >= 0) {
            $scanFiles[] = $fPath;
        }
    }

    foreach ($scanDirs as $pDir) {
        if ($onlyFiles <= 0) {
            $results[] = $pDir;
        }
        if ($maxDepth !== 0) {
            foreach (getDirContents($pDir, $onlyFiles, $excludeRegex, $maxDepth - 1) as $p) {
                $results[] = $p;
            }
        }
    }
    foreach ($scanFiles as $p) {
        $results[] = $p;
    }

    return $results;
}

এবং আপনার যদি আপেক্ষিক পাথের প্রয়োজন হয়:

function updateKeysWithRelPath(array $paths, string $baseDir, bool $allowBaseDirPath = false): array {
    $results = [];
    $regex = '~^' . preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', realpath($baseDir)), '~') . '(?:/|$)~s';
    $regex = preg_replace('~/~', '/(?:(?!\.\.?/)(?:(?!/).)+/\.\.(?:/|$))?(?:\.(?:/|$))*', $regex); // limited to only one "/xx/../" expr
    if (DIRECTORY_SEPARATOR === '\\') {
        $regex = preg_replace('~/~', '[/\\\\\\\\]', $regex) . 'i';
    }
    foreach ($paths as $p) {
        $rel = preg_replace($regex, '', $p, 1);
        if ($rel === $p) {
            throw new \Exception('Path relativize failed, path "' . $p . '" is not within basedir "' . $baseDir . '".');
        } elseif ($rel === '') {
            if (!$allowBaseDirPath) {
                throw new \Exception('Path relativize failed, basedir path "' . $p . '" not allowed.');
            } else {
                $results[$rel] = './';
            }
        } else {
            $results[$rel] = $p;
        }
    }
    return $results;
}

function getDirContentsWithRelKeys(string $dir, int $onlyFiles = 0, string $excludeRegex = '~/\.git/~', int $maxDepth = -1): array {
    return updateKeysWithRelPath(getDirContents($dir, $onlyFiles, $excludeRegex, $maxDepth), $dir);
}

এই সংস্করণটি সমাধান করে / উন্নতি করে:

  1. realpathপিএইচপি ডিরেক্টরি open_basedirকভার করে না এমন থেকে সতর্কতা ..
  2. ফলাফল অ্যারের জন্য রেফারেন্স ব্যবহার করে না
  3. ডিরেক্টরি এবং ফাইল বাদ দিতে দেয়
  4. কেবল ফাইল / ডিরেক্টরি তালিকাতে অনুমতি দেয়
  5. অনুসন্ধান গভীরতা সীমাবদ্ধ করতে দেয়
  6. এটি সর্বদা ডিরেক্টরিগুলির সাথে প্রথমে আউটপুটটি সাজায় (যাতে ডিরেক্টরিগুলি বিপরীত ক্রমে সরানো / খালি করা যায়)
  7. আপেক্ষিক কী সহ পাথ পেতে দেয়
  8. কয়েক হাজার বা এমনকি মিলিয়ন ফাইলের জন্য ভারী অনুকূলিত
  9. মন্তব্যে আরও লিখুন :)

উদাহরণ:

// list only `*.php` files and skip .git/ and the current file
$onlyPhpFilesExcludeRegex = '~/\.git/|(?<!/|\.php)$|^' . preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', realpath(__FILE__)), '~') . '$~is';

$phpFiles = getDirContents(__DIR__, 1, $onlyPhpFilesExcludeRegex);
print_r($phpFiles);

// with relative keys
$phpFiles = getDirContentsWithRelKeys(__DIR__, 1, $onlyPhpFilesExcludeRegex);
print_r($phpFiles);

// with "include only" regex to include only .html and .txt files with "/*_mails/en/*.(html|txt)" path
'~/\.git/|^(?!.*/(|' . '[^/]+_mails/en/[^/]+\.(?:html|txt)' . ')$)~is'
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.