Jump to content

Recommended Posts

I have a media page that I want to automatically update with the audio and corresponding picture.  The audio file will be in folder "audio" named "song1.mp3".  The picture will be in folder "images" named "song1.jpg".  I have figured out the code to automatically update the audio files with a default image, but I want to add the image to the corresponding audio file.  Here is my current code:

 

<?php

$dir=opendir("Audio");

$files=array();

while (($file=readdir($dir)) !== false)

{

if ($file != "." and $file != ".." and $file != "test.php")

{

array_push($files, $file);

}

}

closedir($dir);

sort($files);

foreach ($files as $file)

print "<a class='myPlayer' href='Audio/$file' style='background-image:url(images/logo.jpg)'>

</a>

";

?>

 

So I would want my 'Audio/$file' = to my 'Images/$file' with the same name.  Any help would be appreciated.

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/265627-newbie-question-on-array-of-mp3-files/
Share on other sites

The addition you'd need to make to your code, then, is to read the images directory in the same fashion as you are the audio directory

 

The other piece of this, which I assume is what you're having trouble with, is understanding how to merge the two items in your array and display them.

 

Take a look at building an array that would look like

 

Array
(

    [0] => Array (
        [0] => song1.mp3
        [1] => image1.png
    )
. 
. 
.
)

 

Do you know how to do that?

Guess I am not sure what goes where.  The script I currently have was an example I found and modified it to my needs.  Guess what confuses me is the array_push for both files.  Also was trying to avoid changing the code every time a file is updates which I am assuming I would have to add the audio name and the image name in the array code each time.  Right now here is what the script generates:

 

 

<a class='myPlayer' href='Audio/DannyDuvall032512.mp3' style='background-image:url(images/logo.jpg)'>

 

<img src = '../images/play_small.png' />

 

</a>

 

<a class='myPlayer' href='Audio/DannyDuvall040112.mp3' style='background-image:url(images/logo.jpg)'>

 

<img src = '../images/play_small.png' />

 

</a>

 

There are probably 30 audio files with 3-4 new ones every week.  Just want to match the Audio/DannyDuvall040112.mp3 with the url(images/DannyDuvall040112.jpg)

Here's what I came up with

 

Given

 


nick@avari ~/code/php $ ls -l -R
.:
total 12
drwxr-xr-x 2 nick nick 4096 Jul 13 13:27 audio
drwxr-xr-x 2 nick nick 4096 Jul 13 13:27 images
-rw-r--r-- 1 nick nick 1166 Jul 13 14:02 test.php

./audio:
total 0
-rw-r--r-- 1 nick nick 0 Jul 13 13:27 lol.mp3

./images:
total 0
-rw-r--r-- 1 nick nick 0 Jul 13 13:27 lol.png

 

Output:

 


<a class='myPlayer' href='audio/lol.mp3' style='background-image:url(images/lol.png)'>
            </a>

 

And... here's the code

 


<?php

function directory_to_array($dir) {
    $ret = array();
    $dh = opendir($dir);

    while (($file=readdir($dh)) !== false) {
        if ($file != "." && $file != "..") // ignore . and ..

            $ret[] = $file;
    }

    closedir($dh);

    return $ret;
}

function match_files($audio, $image) {
    //assume basename in $audio and $image are the same
    //merges the two to a new array
    $ret = array();

    foreach ($audio as $name) {
        $abase = explode(".", $name);

        foreach ($image as $img) {
            $ibase = explode(".", $img);

            if ($abase[0] == $ibase[0]) {
                $ret[] = array($name, $img);
            }
        }
    }

    return $ret;

}

function merge_to_player($input) {
    foreach ($input as $pair) {

        print "<a class='myPlayer' href='audio/$pair[0]' style='background-image:url(images/$pair[1])'>
            </a>
            ";
    }

}

//get the audio directory contents
$audio = directory_to_array("audio");
//get the image directory contents
$image = directory_to_array("images");


$matched = match_files($audio, $image);

merge_to_player($matched);

?>

Ok, got it working except for one bug.  It matches all files correctly, but shows a:

 

<a class='myPlayer' href='audio/_hcc_thumbs' style='background-image:url(images/_hcc_thumbs)'>

 

Not seeing that in my directory so not sure how to get rid of it.  Also is there a way I  can reverse the sort?

 

Thanks so much

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.