Results 1 to 10 of 10

Thread: Shuffle and re-write file names in a website directory

  1. #1
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    142
    Thanks
    102
    Thanked 1 Time in 1 Post

    Thumbs up Shuffle and re-write file names in a website directory

    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
    Last edited by KennyP; 07-14-2014 at 10:08 PM.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    2,038
    Thanks
    16
    Thanked 316 Times in 314 Posts
    Blog Entries
    13

    Default

    Untested, but something like this;
    PHP Code:
    <?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); 
    ?>
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    KennyP (07-13-2014)

  4. #3
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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.
    Jeremy | jfein.net

  5. The Following User Says Thank You to Nile For This Useful Post:

    KennyP (07-13-2014)

  6. #4
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    142
    Thanks
    102
    Thanked 1 Time in 1 Post

    Default

    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:
    Code:
    <?
    /* 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:
    Code:
    <?
    /* 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:
    Code:
    <?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>
    Last edited by KennyP; 07-13-2014 at 08:40 AM.

  7. #5
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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 Code:
    <?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?
    Jeremy | jfein.net

  8. The Following User Says Thank You to Nile For This Useful Post:

    KennyP (07-13-2014)

  9. #6
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    142
    Thanks
    102
    Thanked 1 Time in 1 Post

    Default

    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?
    Last edited by KennyP; 07-13-2014 at 08:24 PM.

  10. #7
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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.
    Jeremy | jfein.net

  11. The Following User Says Thank You to Nile For This Useful Post:

    KennyP (07-13-2014)

  12. #8
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    142
    Thanks
    102
    Thanked 1 Time in 1 Post

    Default

    Thank you so much, Nile and Beverly. I will implement the script on the website and will report back.

  13. #9
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    142
    Thanks
    102
    Thanked 1 Time in 1 Post

    Default

    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
    Last edited by KennyP; 07-14-2014 at 08:56 AM.

  14. #10
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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"
    Jeremy | jfein.net

  15. The Following User Says Thank You to Nile For This Useful Post:

    KennyP (07-14-2014)

Similar Threads

  1. Replies: 7
    Last Post: 12-09-2013, 06:54 PM
  2. Replies: 2
    Last Post: 03-13-2012, 09:02 AM
  3. Replies: 4
    Last Post: 10-02-2011, 05:39 PM
  4. Replies: 2
    Last Post: 08-13-2010, 02:59 AM
  5. files in an indexed folder
    By benslayton in forum Other
    Replies: 0
    Last Post: 09-28-2006, 05:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •