1

Have code that right now creates and array of a directory that has text files and gets their modified date and then sorts descending then slices and echos each individually with other parameters.

I am changing it to get the files in a directory and sort by filename which are numeric and then I want it sorted descending then sliced then echoed.

$files_listed = array();
foreach (glob('dir/*.txt') as $quip) {
    $files_listed[filemtime($quip)] = $quip;
}
krsort($files_listed);
$master_arc = array_slice($files_listed, 0, 5, true);

// array
foreach($master_arc as $step) {

$arc = file($step, FILE_IGNORE_NEW_LINES);

// print
include 'echo.php';

}

I tried scandir and nothing showed up

$files_listed = array();
$dir = 'dir/';
$files_listed = scandir($dir, 1);
$master_arc = array_slice($files_listed, 0, 5, true);

// array
foreach($master_arc as $step) {

$arc = file($step, FILE_IGNORE_NEW_LINES);

// print
include 'echo.php';

}

I tried just removing filemtime and it also failed so I am lost.

$files_listed = array();
foreach (glob('dir/*.txt') as $quip) {
    $files_listed = $quip;
}
arsort($files_listed);
$master_arc = array_slice($files_listed, 0, 5, true);

// array
foreach($master_arc as $step) {

$arc = file($step, FILE_IGNORE_NEW_LINES);

// print
include 'echo.php';

}
1
  • try $files_listed[] since you're appending to an array Commented Sep 6, 2019 at 18:55

1 Answer 1

1

This works for me.

<?php

$files_listed = array();

foreach (glob('*.txt') as $quip) {
    $files_listed[] = $quip;
}

arsort($files_listed);
$master_arc = array_slice($files_listed, 0, 5, true);

// array
foreach($master_arc as $step) {
print_r($step);

$arc = file($step, FILE_IGNORE_NEW_LINES);
print_r($arc);

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.