...

View Full Version : Random readdir php to XML



Robbie59
12-21-2006, 10:38 AM
Thanks for viewing my thread. I would like to create a random list of files from a directory. The use of this would be to supply a Flash flv player with a shuffled playlist. Please have a look at this code and maybe you guys can figure out a way to make this php file output a random list of my directory into XML..
The code works great now but Id like it to be random or shuffled on each visit.
THANKS!


<?php

$filter = ".flv";

// path to the directory you want to scan

$directory = "../vids";

// read through the directory and filter files to an array

@$d = dir($directory);

if ($d) {

while($entry=$d->read()) {

$ps = strpos(strtolower($entry), $filter);

if (!($ps === false)) {

$items[] = $entry;

}
}

$d->close();
sort($items);

}
// we'll first add an xml header and the opening tags ..
header("content-type:text/xml;charset=utf-8");

echo "<ratings>\n";
// .. then we loop through the mysql array ..
for($i=0; $i<sizeof($items); $i++) {
echo "<video file=\"$dir$items[$i]\" >\n";
echo " <title>".str_replace(".flv","",$items[$i])."</title>\n";
echo " <runtime>auto</runtime>\n";
echo " </video>\n";
}
echo "</ratings>\n";

?>


THE OUTPUT LOOKS LIKE THIS:


<ratings>

<video file="video1.flv">
<title>video1</title>
<runtime>auto</runtime>
</video>

<video file="video2.flv">
<title>video2</title>
<runtime>auto</runtime>
</video>

<video file="video3.flv">
<title>video3</title>
<runtime>auto</runtime>
</video>

NancyJ
12-21-2006, 03:38 PM
Since you've already got your stuff in an array just shuffle() instead of sort()

http://uk2.php.net/manual/en/function.shuffle.php

Robbie59
12-21-2006, 05:43 PM
Since you've already got your stuff in an array just shuffle() instead of sort()

http://uk2.php.net/manual/en/function.shuffle.php

Great, so you think this is possible? How should I code this to make it shuffle? Can you help me get started on where to add this code?

Thanks!

Robbie59
12-21-2006, 06:17 PM
sort($items);

Too easy, thanks for the kick start! Problem solved



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum