Subject: How to write files information to mysql db using p
Posted By: method Post Date: 2/8/2006 10:49:58 AM
Hi all .I wonder how i can write the file path and file name that i read from mp3 folder and write them to mysql database table. Currently print_r($playlist); outputs the file names with .mp3 extention as shown below .Furthermore,print_r(htmlentities($code)) output path and file title as shown below. I be happy if an expert show me how i can write the file name and file path and write them to mysql db with file number.Thanks

<?php

// setting the directory to search for mp3 files
$dir = "mp3/";

// reading the directory and inserting the mp3 files in the playlist array
$n = 0;
$playlist = array();
$fdir = opendir($dir);
while($i = readdir($fdir)) {
   // if a .mp3 string is found, add the file to the array
   if (strpos(strtolower($i),".mp3") !== false) {
         $playlist[$n] = $i;
         $n++;
      } 
}
// close the directory and sort the array
closedir($fdir);
array_multisort($playlist);

print_r($playlist);

// echoing the playlist to flash
 $code = "<player showDisplay=\"yes\" showPlaylist=\"no\" autoStart=\"yes\">\n";
 for ($i=0; $i<sizeof($playlist); $i++) {
   // for the title it filters the directory and the .mp3 extension out
    $code .= "  <song path=\"$dir.$playlist[$i]\" title=\"".str_replace(".mp3","",$playlist[$i])."\"  />\n";
 }
 $code .= "</player>";

print_r(htmlentities($code));
?>


print_r($playlist); output
Array ( 

[0] => songname1.mp3 
[1] => songname2.mp3 
[2] => songname3.mp3 
[3] => songname4.mp3 
[4] => songname5.mp3 
[5] => songname6.mp3 
[6] => songname7.mp3 
[7] => songname8.mp3 
 => songname9.mp3 
[9] => songname10.mp3 
[10] => songname11.mp3 
[11] => songname12.mp3 
)


print_r(htmlentities($code)); output
<player showDisplay="yes" showPlaylist="no" autoStart="yes"> 
<song path="mp3/songname1.mp3" title="songname1" /> 
<song path="mp3/songname2.mp3" title="songname2" />
.
.
.
 </player>
Reply By: Mantis Reply Date: 2/12/2006 6:10:49 PM
My SQL input output data is pretty straight foward.. your code looks pretty nice so whats is the issue ??

let me re explain this:

You have done all the hard work in your code. I can't see where the difficulty is for you ???

Thats pretty self evident for example:
<player showDisplay="yes" showPlaylist="no" autoStart="yes">
<song path="mp3/<?php echo $songname1; ?>" title="<?php echo $songname1; ?>" />
<song path="mp3/<?php echo $songname2; ?>" title="<?php echo $songname2; ?>" />
.
.
.
 </player>

???
Reply By: method Reply Date: 2/12/2006 6:46:42 PM
quote:
Originally posted by Mantis

My SQL input output data is pretty straight foward.. your code looks pretty nice so whats is the issue ??

let me re explain this:

You have done all the hard work in your code. I can't see where the difficulty is for you ???

Thats pretty self evident for example:
<player showDisplay="yes" showPlaylist="no" autoStart="yes">
<song path="mp3/<?php echo $songname1; ?>" title="<?php echo $songname1; ?>" />
<song path="mp3/<?php echo $songname2; ?>" title="<?php echo $songname2; ?>" />
.
.
.
 </player>

???



There a few problem that i do not how do it. I want to write mp3/songname1.mp3(song path) and songname(title to)my sql database for each file in the mp3 directory.Furthermore my code does not read files inside mp3 subdirectory (mp3/subfolder).I be happy if u help me fix these problems.Thanks
Reply By: Mantis Reply Date: 2/14/2006 12:22:09 PM
I don't know why you want to store the filename in a database to be honnest.. not my problem you would say. lol

I found this nice Mp3 flash player this morning:
http://www.jeroenwijering.com/

and I put a bit of code together which happen to be close to what you need (lucky you!)
<?php
$NBFile=0;
$NBDir=0;
$mp3folder="mp3";

if ($handle = opendir($mp3folder)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "..") {

if ( strlen($file) >= 3 ) {

if ( substr($file, strlen($file)-4,1) != "." ) {
        $DirArray[] = $file;
        $NBDir=$NBDir+1;
} }

        }
    }
}
closedir($handle);
sort($DirArray);
$NBDir=$NBDir-1;

for ($d=0; $d<=$NBDir; $d++) {
$folder_Name1=$mp3folder."/".$DirArray[$d];


if ($handle = opendir($folder_Name1)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        if (substr($file, strlen($file)-3, 3)=="mp3" ) {
            $FileArray[] = $folder_Name1."/".$file;
            $DirArray2[] = $folder_Name1;
            $NBFile=$NBFile+1;
        }            

        }
    }
}
closedir($handle);

if ($handle = opendir($folder_Name1)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        if (substr($file, strlen($file)-3, 3)=="MP3" ) {
            $FileArray[] = $folder_Name1."/".$file;
            $DirArray2[] = $folder_Name1;
            $NBFile=$NBFile+1;
        }            

        }
    }
}
closedir($handle);
sort($FileArray);
sort($DirArray2);
}

if ($handle = opendir($mp3folder)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        if (substr($file, strlen($file)-3, 3)=="mp3" ) {
            $FileArray[] = $mp3folder."/".$file;
            $DirArray2[] = $mp3folder;
            $NBFile=$NBFile+1;
        }            

        }
    }
}
closedir($handle);

if ($handle = opendir($mp3folder)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        if (substr($file, strlen($file)-3, 3)=="MP3" ) {
            $FileArray[] = $mp3folder."/".$file;
            $DirArray2[] = $mp3folder;
            $NBFile=$NBFile+1;
        }            

        }
    }
}
closedir($handle);


$NBFile=$NBFile-1;
$data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"."\n";
$data=$data."<player showDisplay=\"yes\" showPlaylist=\"no\" autoStart=\"yes\">"."\n";

for ($i=0; $i<=$NBFile; $i++) {
$file_Name1=$FileArray[$i];
$file_Name2=$DirArray2[$i];
$file_Title=substr($file_Name1, strlen($file_Name2)+1, strlen($file_Name1)-5-strlen($file_Name2));
$data=$data."<song path=\"".$file_Name1."\" title=\"".$file_Title."\" />"."\n";
}
$data=$data."</player>";

$fp = fopen("mp3player.xml", "w");
fwrite($fp, $data);
fclose($fp);?>


<html>
<head>
<title>JW Flash MP3 Player</title>
</head>
<body bgcolor="#C0C0C0">
<p align="center">

<object type="application/x-shockwave-flash" data="mp3player.swf" width="300" height="300" wmode="transparent">
  <param name="movie" value="mp3player.swf" />
  <param name="wmode" value="transparent" />
</object>
</p>
<p align="center" style="margin-top: 200; margin-bottom: 0">
<font face="Arial" size="2"><i>Credit to Jeroen Wijering for This MP3
Flashplayer. </i></font></p>
<p align="center" style="margin-top: 0; margin-bottom: 0"><i>
<font face="Arial" size="2"><a target="_blank" href="http://www.jeroenwijering.com/">
http://www.jeroenwijering.com/</a></font></i></p>

</body>
</html>

If you still want to place the title / song name in the database, you need to create a database table with a minimum of 3 fields: ID . Name . Title

If you look at the code the file name / directory is in the Array.
Reply By: method Reply Date: 2/14/2006 8:18:50 PM
Many Many thanks for your nice code. I see you did a good job in scanning all files encluding sub foldrs which i wanted to know. I assume it should work for all folders inside mp3.To what deapth it will read mp3?mp3\subfolder1\subfolder1 or just mp3\subfolder\? May i know why the code below is repeated a few times :

if ($handle = opendir($folder_Name1)) {
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != "..") {
        if (substr($file, strlen($file)-3, 3)=="MP3" ) {
            $FileArray[] = $folder_Name1."/".$file;
            $DirArray2[] = $folder_Name1;
            $NBFile=$NBFile+1;
        }            

        }
    }
}

I be happy if u explain to me the logic of code and how it scans all sub folders?

Suppose i want the user to select a few songs and play his own playlist by player?php=1,2,3.Now u might know why i am thinking of database!At that stage writing playlist in xml might be problomatic .What might happend if 2 user at the same time use the script and both get same songs!! How about dealing Browserer Cache at that point? It was nice if u can show me how i can avoid using xml and just echo all the songs to flash directly .




Go to topic 38041

Return to index page 369
Return to index page 368
Return to index page 367
Return to index page 366
Return to index page 365
Return to index page 364
Return to index page 363
Return to index page 362
Return to index page 361
Return to index page 360