About file sorting

Hello,
I am sorting by file creation date with the following code
No problems with ranking
I want to add sorting by file type to this
how do i do this

$tum_yedekler_dizisi = array();
$dizin_yolu = new FilesystemIterator($dizin);

foreach($dizin_yolu as $dizindeki_dosya) {
    if($dizindeki_dosya->getFilename() != '.htaccess' && $dizindeki_dosya->getFilename() != 'index.html'){
        $tum_yedekler_dizisi[$dizindeki_dosya->getMTime()][] = $dizindeki_dosya->getFilename();
    }
}
krsort($tum_yedekler_dizisi);
$tum_yedekler_dizisi = call_user_func_array('array_merge', $tum_yedekler_dizisi);

Are your files documented in your database? It is much easier to sort with a query.
If you need to do fancy sorts, you can do it using PHP functions build for that.
https://www.php.net/manual/en/function.array-multisort.php
You can use these functions, but, you would need to create a multidemensional array with the parts of the file in columns. Like extension and creation date and then sort one two columns. Should work.

No, not query from database
I list .sql, .gz and folder in directory
First I want to list files by creation date
Next, the folders will be listed first, then the gz files, then the sql files.

With the code above, I am listing by file creation date.
Ekran görüntüsü 2021-11-22 123521
I want to sort like in the picture
However, I did this using a separate foreach for each file type.
So I’m using 3 foreach for an array

sql and is_dir()

foreach($tum_yedekler_dizisi AS $vals){
    $filetype = pathinfo($vals, PATHINFO_EXTENSION);
    if(file_exists($dizin.$vals) && $filetype == 'gz'){
	
	}
}

Well, if you just keep the names and dates in an array, you can sort by any column/field using column sorts.
You can use array_column and array_multisort functions to sort by any column.

Or to make it easier, when you parse thru the file list and create an array of the names, just create a second array for the dates. They use two foreach loops to display each.

1 Like

All codes in first post

All files in the directory are imported as arrays.

$dizin = "folder/";
$dizin_yolu = new FilesystemIterator($dizin);

File creation dates are added to the array with the following code.

$tum_yedekler_dizisi[$dizindeki_dosya->getMTime()][]

Files are sorted by creation date.

krsort($tum_yedekler_dizisi);

The array is recreated by removing the file creation dates from the array.

$tum_yedekler_dizisi = call_user_func_array('array_merge', $tum_yedekler_dizisi);

Now we have an array sorted by file creation date.

I’m already parsing 3 file types, and I think 3 foreach will be ok
I wanted to know if it can be parsed, maybe by adding something simple.
Thank you

Sponsor our Newsletter | Privacy Policy | Terms of Service