FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » randomly sorting files in php
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: randomly sorting files in php [message #180590 is a reply to message #180581] Mon, 04 March 2013 02:59 Go to previous messageGo to previous message
Cal Dershowitz is currently offline  Cal Dershowitz
Messages: 36
Registered: February 2013
Karma:
Member
On 02/28/2013 10:54 PM, David Robley wrote:

> Have a look at these functions for starters:
>
> rand()
> array_rand()
> shuffle()
>

Alright, David, I'm pleased with how far I've gotten with this so far

$ pwd
/var/www
$ ls
CCE0-705F index.html read_dir1.php read_dir3.php read_dir5.php
date.php music1 read_dir1.php~ read_dir3.php~ read_dir5.php~
date.php~ music1.sh read_dir2.php read_dir4.php test.php
hello.php music1.sh~ read_dir2.php~ read_dir4.php~ Untitled Document
$ cat read_dir4.php
<?php

function getFilesFromDir($dir) {

$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(is_dir($dir.'/'.$file)) {
$dir2 = $dir.'/'.$file;
$files[] = getFilesFromDir($dir2);
}
else {
$files[] = $dir.'/'.$file;
}
}
}
closedir($handle);
}

return array_flat($files);
}

function array_flat($array) {

foreach($array as $a) {
if(is_array($a)) {
$tmp = array_merge($tmp, array_flat($a));
}
else {
$tmp[] = $a;
}
}

return $tmp;
}

// Usage
$dir = 'music1';
$foo = getFilesFromDir($dir);

print_r($foo);
?>
$

This is my fourth version of this script, obviously written primarily by
someone with more experience.

The output is tremendous, and the next step is to use a regex to knock
off leading numbers and underscores. I have a simple question.

Given that you have the data in an array that is instantiated like this,
how does one use best php form to use a regex on the whole thing?
--
Cal
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to avoid the use of session variables in this script
Next Topic: all done
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 02:51:09 GMT 2024

Total time taken to generate the page: 0.04449 seconds