View Full Version : Resolved Shuffle and re-write file names in a website directory
KennyP
07-13-2014, 02:02 AM
Hi guys,
Is it possible, with php, to shuffle and re-write the numbers/names of a list of songs in a website directory?
That is, in a website folder I have songs named "1.mp3" through "22.mp3"
Can a php script be made to automatically run on page load (or at least once a day) that can shuffle and re-write
the numbers of the mp3 files?
Thanks,
Kenny
Beverleyh
07-13-2014, 06:16 AM
Untested, but something like this;
<?php
$dir = '/home/www/mywebsite.com/mp3s/'; // root path to files
$exts = '/\.(mp3)$/'; // only these file types (mp3|mp4|wma)
if ($hd = opendir($dir)) {
while (false !== ($fname = readdir($hd))) {
if (preg_match('/^\.{1,2}$/', $fname)) continue; // exclude current directory, parent directory
if (!preg_match($exts, $fname)) continue; // exclude file types not in $exts
$files_array[] = $dir.$fname;
}
}
shuffle($files_array); // shuffle
foreach($files_array as $track) {
$track = str_replace($dir, '', $track); // just track name
echo "<a href='http://www.mywebsite.com/music/$track">$track</a>\n";
}
closedir($hd);
?>
Beverly's script will only print out a list, shuffling the names. Is that what you wanted? I assumed you actually wanted the files renamed.
KennyP
07-13-2014, 07:34 AM
Thank you Beverly and Nile for your replies.
Yes, I actually would like the files renamed, so that a flash site's built-in player will play them randomly.
Otherwise, I would like to use the following script I found to read the mp3 file names in a directory, and then output an updated xml file called "options.xml," from which the player can read them.
Please tell me which is the best way to do it?
Script I found:
<?
/* directory to go through */
$file_dir = 'mp3';
/* temporary storage for the filenames */
$files = array();
/* open directory */
$dir = opendir($file_dir);
/* loop through directory */
while ( $file = readdir($dir) )
{
if ($file != '.' && $file != '..')
{
/* push into array of files */
array_push( $files, $file );
}
}
/* shuffle the order of filenames in the array */
shuffle( $files );
/* print beginning of XML doc */
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
echo '<songs>'."\n";
/* loop through files */
foreach( $files as $file ) {
list($name, $ext) = explode('.', $file);
list($artist, $title) = explode('-', $name);
echo "\t".'<song path="'.$file.'" artist="'.$artist.'" title="'.$title.'"></song>'."\n";
}
/* close XML file */
echo '</songs>'."\n";
?>
My adaption of the script:
<?
/* directory to go through */
$file_dir = '/music';
/* temporary storage for the filenames */
$files = array();
/* open directory */
$dir = opendir($file_dir);
/* loop through directory */
while ( $file = readdir($dir) )
{
if ($file != '.' && $file != '..')
{
/* push into array of files */
array_push( $files, $file );
}
}
/* shuffle the order of filenames in the array */
shuffle( $files );
/* print beginning of XML doc */
echo '<?xml version="1.0" encoding="UTF-8"?>
<options>
<general
websitetitle="Guy Arseneau"
headerminheight="120"
headermaxheight="150"
bodybgcolor="0xffffff"
bodybgalpha="0.1"
bodybgblur="15"
bodybgtoppadding="20"
bodybgrounded="10"
modulebgcolor="0x000000"
modulebgalpha="0.4"
templatewidth="980"
templateheight="555"
logourl="logo/logo.png"
logoorientation="left"
logoheight="120"
logopadding="-20"
logoclicklink="index.html#/home"
logoclicktarget="_top"
menufont="Arial"
menufontbold="true"
textfont="Arial"
menualign="center"
menubarcolor="0x000000"
menubaralpha="0.4"
menubarheight="40"
menubarpadding="30"
menubarrounded="10"
menufontsize="17"
menuspacing="22"
menuidlecolor="0xcccccc"
menuselectcolor="0xFFBE59"
menushadowdistance="0"
menushadowstrength="0"
menushadowopacity="0"
submenubgcolor="0x000000"
submenubgopacity="0.7"
submenufontsize="14"
submenupadding="25"
submenuspacing="10"
submenubottomrounded="10"
submenuidlecolor="0xaaaaaa"
submenuselectcolor="0xffffff"
submenubgshadowdistance="3"
submenubgshadowstrength="5"
submenubgshadowopacity="0.1"
moduletoppadding="0"
footerheight="40"
footerpadding="30"
footerbottompadding="10"
footerbgopacity="0.6"
footerbgrounded="10"
footerbgcolor="0x000000"
footerpixelopacity="0"
footerpixelcolor="0x000000"
footershadowdistance="0"
footershadowstrength="0"
footershadowopacity="0"
footertextshadowdistance="0"
footertextshadowstrength="0"
footertextshadowopacity="0"
musicfullscreenrightpadding="10"
musicfullscreenbottompadding="0"
musicfullscreencolor="0xaaaaaa"
musicfullscreenopacity="0.4"
/>'."\n";
echo '<musicentries
musicactivated="true"
musicvolumecolor="0xffb66e"
musicvolumebgcolor="0x2f2924"
musiciconcolor="0xffffff"
musicbuttoncolor="0x1e1a17"
musicstartvolume="0.30"
musicplayerwidth="250"
musicplayerpadding="10"
musicplayerbgopacity="0.9"
musicplayertoprounded="10"
musicvolumeheight="30"
musicentryheight="24"
musicentryfontsize="11"
musicentryfontcolor="0xaaaaaa"
musicentrybgcolor="0x1e1a17"
musicentrybgopacity="0.4"
>'."\n";
/* loop through files */
foreach( $files as $file ) {
list($name, $ext) = explode('.', $file);
// list($artist, $title) = explode('-', $name);
echo "\t".'<song urllink="'.$file.'"> Select - track<title="'.$title.'"></song>'."\n";
}
/* close XML file */
echo '</musicentries>
<footertext>
<![CDATA[
<ft>Copyrighted 2014 by Guy Arseneau, New York City, USA, All Rights Reserved</ft>
]]>
</footertext>
</options>
'."\n";
?>
The page, options.xml, I need to output:
<?xml version='1.0' encoding='utf-8'?>
<options>
<general
websitetitle="Guy Arseneau"
headerminheight="120"
headermaxheight="150"
bodybgcolor="0xffffff"
bodybgalpha="0.1"
bodybgblur="15"
bodybgtoppadding="20"
bodybgrounded="10"
modulebgcolor="0x000000"
modulebgalpha="0.4"
templatewidth="980"
templateheight="555"
logourl="logo/logo.png"
logoorientation="left"
logoheight="120"
logopadding="-20"
logoclicklink="index.html#/home"
logoclicktarget="_top"
menufont="Arial"
menufontbold="true"
textfont="Arial"
menualign="center"
menubarcolor="0x000000"
menubaralpha="0.4"
menubarheight="40"
menubarpadding="30"
menubarrounded="10"
menufontsize="17"
menuspacing="22"
menuidlecolor="0xcccccc"
menuselectcolor="0xFFBE59"
menushadowdistance="0"
menushadowstrength="0"
menushadowopacity="0"
submenubgcolor="0x000000"
submenubgopacity="0.7"
submenufontsize="14"
submenupadding="25"
submenuspacing="10"
submenubottomrounded="10"
submenuidlecolor="0xaaaaaa"
submenuselectcolor="0xffffff"
submenubgshadowdistance="3"
submenubgshadowstrength="5"
submenubgshadowopacity="0.1"
moduletoppadding="0"
footerheight="40"
footerpadding="30"
footerbottompadding="10"
footerbgopacity="0.6"
footerbgrounded="10"
footerbgcolor="0x000000"
footerpixelopacity="0"
footerpixelcolor="0x000000"
footershadowdistance="0"
footershadowstrength="0"
footershadowopacity="0"
footertextshadowdistance="0"
footertextshadowstrength="0"
footertextshadowopacity="0"
musicfullscreenrightpadding="10"
musicfullscreenbottompadding="0"
musicfullscreencolor="0xaaaaaa"
musicfullscreenopacity="0.4"
/>
<musicentries
musicactivated="true"
musicvolumecolor="0xffb66e"
musicvolumebgcolor="0x2f2924"
musiciconcolor="0xffffff"
musicbuttoncolor="0x1e1a17"
musicstartvolume="0.30"
musicplayerwidth="250"
musicplayerpadding="10"
musicplayerbgopacity="0.9"
musicplayertoprounded="10"
musicvolumeheight="30"
musicentryheight="24"
musicentryfontsize="11"
musicentryfontcolor="0xaaaaaa"
musicentrybgcolor="0x1e1a17"
musicentrybgopacity="0.4"
>
<song
urllink="music/1.mp3"
>
Select - Track 1
</song>
<song
urllink="music/2.mp3"
>
Select - Track 2
</song>
<song
urllink="music/3.mp3"
>
Select - Track 3
</song>
<song
urllink="music/4.mp3"
>
Select - Track 4
</song>
<song
urllink="music/5.mp3"
>
Select - Track 5
</song>
<song
urllink="music/6.mp3"
>
Select - Track 6
</song>
<song
urllink="music/7.mp3"
>
Select - Track 7
</song>
<song
urllink="music/8.mp3"
>
Select - Track 8
</song>
<song
urllink="music/9.mp3"
>
Select - Track 9
</song>
<song
urllink="music/10.mp3"
>
Select - Track 10
</song>
<song
urllink="music/11.mp3"
>
Select - Track 11
</song>
<song
urllink="music/12.mp3"
>
Select - Track 12
</song>
<song
urllink="music/13.mp3"
>
Select - Track 13
</song>
<song
urllink="music/14.mp3"
>
Select - Track 14
</song>
<song
urllink="music/15.mp3"
>
Select - Track 15
</song>
<song
urllink="music/16.mp3"
>
Select - Track 16
</song>
<song
urllink="music/17.mp3"
>
Select - Track 17
</song>
<song
urllink="music/18.mp3"
>
Select - Track 18
</song>
<song
urllink="music/19.mp3"
>
Select - Track 19
</song>
<song
urllink="music/20.mp3"
>
Select - Track 20
</song>
<song
urllink="music/21.mp3"
>
Select - Track 21
</song>
<song
urllink="music/22.mp3"
>
Select - Track 22
</song>
</musicentries>
<footertext>
<![CDATA[
<ft>Copyrighted 2014 by Guy Arseneau, New York City, USA, All Rights Reserved</ft>
]]>
</footertext>
</options>
I'm not quite sure of your question above, but the following script should effectively shuffle the names of all your files in a directory that match the specified pattern on line 3:
<?php
$files = glob( "*.mp3" ); // desired pattern
$shuffled = $files;
shuffle( $shuffled );
foreach( $files as $key => $filename )
{
rename( $filename, $filename . "-old" );
}
foreach( $files as $key => $filename )
{
rename( $filename . "-old", $shuffled[$key] );
}
It seems a bit roundabout to be doing this in PHP when you could shuffle the order the songs are played in Actionscript, but who uses Actionscript anyways these days?
KennyP
07-13-2014, 05:50 PM
Thank you Nile, that is exactly what I want to do, but how is it set up?
Doesn't it need a path added to the MP3 directory, as Beverly wrote?
Also, can the script be made to run either on page load or at least once a day, without setting up a cron job?
The paths are relative, so changing what's inside of the double quotes on the third line acts as your path. If the PHP file is in the same directory as all the mp3s, you don't need to change anything. If it isn't, you'll need to change the path (what's inside the double quotes is equal to Beverly's $dir variable, but with the addition of a file pattern, e.g., "*.mp3").
The script does run on page load, so that's covered. To assure that it runs least once a day you'll need a cron job or something like.
KennyP
07-13-2014, 07:26 PM
Thank you so much, Nile and Beverly. I will implement the script on the website and will report back.
KennyP
07-14-2014, 07:39 AM
Thank you, it works just great. Whatever number of mp3's I upload into the music directory get shuffled perfectly on every page load.
Thank you again,
Kenny
Glad it's working for you.
In an effort to keep things organized, you have the option to set a thread to resolved when an issue is fixed. To make the status of the thread resolved:
1. Go to your first post
2. Edit your first post
3. Click "Go Advanced"
4. In the dropdown next to the title, select "RESOLVED"
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.